How can I program a decimal addition with JavaScript
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.
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.
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);
});
精彩评论