0

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

000
  • 26,951
  • 10
  • 71
  • 101
trajce
  • 1,480
  • 3
  • 23
  • 38

5 Answers5

2

Use decimal point . instead of comma (,) 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 commas for some reason you can replace commas with points by code: (thiis will work for both point and comma useage)

var txtQuantity = $('.txtQuantity').val().replace(/,/g,'.');
var txtRate = $('.txtRate').val().replace(/,/g,'.');
Yotam Omer
  • 15,310
  • 11
  • 62
  • 65
2

You have a comma 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>
DFord
  • 2,352
  • 4
  • 33
  • 51
2

You're using commas instead of decimal points. Change to:

<input type="text" value="12.3" class="txtRate" />

Fiddle: http://jsfiddle.net/peAZJ/6/

Luke
  • 1,243
  • 10
  • 10
1

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 comma:

<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>
sirmdawg
  • 2,579
  • 3
  • 21
  • 32
1
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;