-1

Please note, I am suprised with the below problem, I have two values to be divided in TextBox T1 and T2. I am dividing the same to get Amount And not Getting the Exact Amount. Rather then getting the Amount in Fraction 00000001.

Example:
 var t1=5623.52;
 var t2=56.2352;
 var t3=5623.52/56.2352; //100.0000000001

Note: I can't round up the values since the vales are Exchange Rates so vary according to currency.

alex
  • 479,566
  • 201
  • 878
  • 984
Sravan
  • 1,095
  • 4
  • 16
  • 27

2 Answers2

1

This is caused by the limited precision of floating point values. See The Floating Point Guide for full details.

The short version is that the 0.52 fractional part of your numbers cannot be represented exactly in binary, just like 1/3 cannot be represented exactly in decimal. Because of the limited number of digits of accuracy, the larger number is slightly more precise than the smaller one, and so is not exactly 100 times as large.

If that doesn't make sense, imagine you are dealing with thirds, and pretend that numbers are represented as decimals, to ten decimal places. If you declare:

var t1 = 1000.0 / 3.0;
var t2 = 10.0 / 3.0;

Then t2 is represented as 3.3333333333, which is as close as can be represented with the given precision. Something that is 100 times as large as t2 would be 333.3333333300, but t1 is actually represented as 333.3333333333. It is not exactly 100 times t2, due to rounding/truncation being applied at different points for the different numbers.


The fix, as always with floating-point rounding issues, is to use decimal types instead. Have a look at the Javascript cheat-sheet on the aforementioned guide for ways to go about this.

Andrzej Doyle
  • 102,507
  • 33
  • 189
  • 228
  • Thanks for your solution, But as the Cheat sheet suggests, the parseFloat() does not return the required value 100. rather we can't use the toPrecision() or toFixed() to round the floating point numbers. – Sravan Jun 21 '13 at 06:47
0

like Felix Kling said, don't use floating point values. Or use parseInt if you want to keep an integer.

 var t1=5623.52;
 var t2=56.2352;
 var t3=parseInt(t1/t2);
AntouanK
  • 4,880
  • 1
  • 21
  • 26