admin管理员组文章数量:1410730
im experiencing a really weird problem. Im trying to multiply 2 decimal values, but the result i get is an integer/rounded number. If i add decimals (for example toFixed(2), it just adds some zeroes).
Here is the script:
$(document).ready(function () {
$('.txtQuantity, .txtRate').change(function () {
var txtQuantity = $('.txtQuantity').val();
var txtRate = $('.txtRate').val();
var total = parseFloat(txtQuantity) * parseFloat(txtRate);
$('.txtTotal').val(total);
});
});
and here is the html:
<table>
<tr>
<td><input type="text" class="txtQuantity" /></td>
<td><input type="text" value="12,3" class="txtRate" /></td>
<td><input type="text" disabled="disabled" class="aspNetDisabled txtTotal" /></td>
</tr>
</table>
here is a fiddle
What am i missing? it must be something trivial.
Thanks
im experiencing a really weird problem. Im trying to multiply 2 decimal values, but the result i get is an integer/rounded number. If i add decimals (for example toFixed(2), it just adds some zeroes).
Here is the script:
$(document).ready(function () {
$('.txtQuantity, .txtRate').change(function () {
var txtQuantity = $('.txtQuantity').val();
var txtRate = $('.txtRate').val();
var total = parseFloat(txtQuantity) * parseFloat(txtRate);
$('.txtTotal').val(total);
});
});
and here is the html:
<table>
<tr>
<td><input type="text" class="txtQuantity" /></td>
<td><input type="text" value="12,3" class="txtRate" /></td>
<td><input type="text" disabled="disabled" class="aspNetDisabled txtTotal" /></td>
</tr>
</table>
here is a fiddle
What am i missing? it must be something trivial.
Thanks
Share Improve this question edited Jul 17, 2013 at 14:35 000 27.3k10 gold badges74 silver badges103 bronze badges asked Jul 17, 2013 at 14:29 trajcetrajce 1,4903 gold badges23 silver badges38 bronze badges 3-
You are using a
,
in the fiddle when you should be using a decimal point – turnt Commented Jul 17, 2013 at 14:31 - Isn't the ma as a decimal point usually a currency thing in Europe? – j08691 Commented Jul 17, 2013 at 14:34
- 1 Please see this related question for some insight and ideas: stackoverflow./q/5314237/1253312 – 000 Commented Jul 17, 2013 at 14:36
5 Answers
Reset to default 2Use decimal point .
instead of ma (,
) for parseFloat to read the number properly.. working jsFiddle
<input type="text" value="12.3" class="txtRate" />
You should also use the decimal point when you insert the number to the other input. If you still want to use mas for some reason you can replace mas with points by code: (thiis will work for both point and ma useage)
var txtQuantity = $('.txtQuantity').val().replace(/,/g,'.');
var txtRate = $('.txtRate').val().replace(/,/g,'.');
You have a ma where it should be a point.
<table>
<tr>
<td><input type="text" class="txtQuantity" /></td>
<td><input type="text" value="12.3" class="txtRate" /></td>
<td><input type="text" disabled="disabled" class="aspNetDisabled txtTotal" /></td>
</tr>
</table>
You're using mas instead of decimal points. Change to:
<input type="text" value="12.3" class="txtRate" />
Fiddle: http://jsfiddle/peAZJ/6/
If you'd like to round to just two decimal places you would use something like this:
Math.round(num * 100) / 100;
But the fiddle seems to work just fine if you remove the ma:
<td><input type="text" value="12,3" class="txtRate" /></td>
that should look like this:
<td><input type="text" value="12.3" class="txtRate" /></td>
var decvalue = 0.07;
var originalvalue = decvalue * 100;
var wholevalue = Math.floor(originalvalue);
var finalvalue;
if ((originalvalue.toFixed(2) - wholevalue) > 0) {
finalvalue = originalvalue.toFixed(2);
}
else
finalvalue = wholevalue;
本文标签: javascriptMultiplication of decimal valuesStack Overflow
版权声明:本文标题:javascript - Multiplication of decimal values - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744270990a2598181.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论