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
Add a ment  | 

5 Answers 5

Reset to default 2

Use 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