views:

52

answers:

2

How can i program this, the decimal shift needs to be programed that most of it I think , the rest would be just a normal add, any thoughts on how to program this? This is for an incrementer I'm building, the user can press plus or minus and increment a text input.

A: 

You can use:

HTML

<input id="value" type="text" value="0">
<input id="right" type="button" value="+">
<input id="left" type="button" value="-">

JS (with jquery)

$("#right").click(function(){
$("#value").val($("#value").val() * 10);
});

$("#left").click(function(){
$("#value").val($("#value").val() / 10);
});
Topera
yes but it needs to show like this: 10,00 so you can add 10,25 + 100,10 for example
tada
Sorry, but still confusing...
Topera
it's addition but with comma , i need to show and add monetary values , so theres a need to program the comma shifting
tada
If you want a button to shift comma to rigth or left, you need just multiply by 10 ou divide by 10. I'll edit my aswser.
Topera
it's not a button, the shift needs to be programed, it's automatical just like in a normal calculator
tada
Sorry, but I don't understand. If you sum 3.1 with 10.12, you will have 13.22. You don't need to program comma shift... :(
Topera
@tada your request is very confusing - can you indicate all the input elements + buttons you have and a few sample additions you want to accomplish? (edit this information and add it to the original question)
scunliffe
@Topera , true, thanks for your help.@scunliffe, I think i found a solution.regards.
tada
A: 

I assume you're referring to the use of a comma , as a decimal separator instead of a ..

If there are no other commas used as separators in the number (I don't know what is used for thousand separators in that case), then you could try something like this:

Try it out: http://jsfiddle.net/yuEAs/

HTML

<input id='one' type='text' value='12,34' /><br>
<input id='two' type='text' value='43,21' /><br>
<input id='result' type='text' /><br>
<div id='click'>Click to add</div>

jQuery

$('#click').click(function() {
       // Replace comma separators with point separators, and parseFloat the result
    var one = parseFloat($('#one').val().replace(',','.'));
    var two = parseFloat($('#two').val().replace(',','.'));
       // Add the numbers
    var result = one + two;
       // Convert the result to a string, and revert the decimal separator back
    $('#result').val( result.toString().replace('.',',') );
});

​I don't have any experience with this, so maybe there's a better way.

patrick dw