0

Please refer below code.

    currentAxis.Range.Start = 1956.9

    tempInterval = currentAxis.Range.Start

    currentAxis.Range.Interval = 0.4

    tempInterval += currentAxis.Range.Interval;

It will return the value 1957.3000000000001 instead of 1957.3. I don't want to use the toFixed() method to remove the decimal values in point, because some times the interval value will be 0.0004 that time it will return 4 digits after the point. Based on the interval it will vary.

I need 1956.9 +0.4= 1957.3 with out using toFixed() method. How can i solve this?

George
  • 36,413
  • 9
  • 66
  • 103
SivaRajini
  • 7,225
  • 21
  • 81
  • 128
  • Does [this question](http://stackoverflow.com/questions/7342957/how-do-you-round-to-1-decimal-place-in-javascript) help you? – George Mar 25 '13 at 09:43
  • no. if am adding single digit point value , the output also will contains same .like "0.4" is single digit so it will add with X and returns "19.X" single digit after the point. no need "toFixed(x) or some calculation ? why this problem occurs in javascript – SivaRajini Mar 25 '13 at 09:50
  • Floating point standards bear that much of rounding error... http://stackoverflow.com/questions/588004/is-javascripts-floating-point-math-broken – loxxy Mar 25 '13 at 09:52
  • That's javascript. Numerical accuracy is not guaranteed. – Beetroot-Beetroot Mar 25 '13 at 10:06
  • That's not a JavaScript problem, that's how floating point arithmetic works. If you don't want to perform any computation, then there is no way to solve your problem. – Felix Kling Mar 25 '13 at 10:17

2 Answers2

0

Try Multiply by 10, fix to decimal & divide by 10.

Using :

Math.round((1956.9 +0.4)*10)/10           // Output 1957.3

or simply :

(((1956.9 + 0.4)*10)<<0)/10               // Output 1957.3
loxxy
  • 12,990
  • 2
  • 25
  • 56
  • i dont want to perform calculation. because (1956.9 +0.0004) it will return 4 digits after the point. based on the values we are going to add with "X" it will vary. no need to use"toFixed(x) or some manual calculation ? how can i solve this ? – SivaRajini Mar 25 '13 at 09:52
0

As already said, this uncertainty is inherent in floating point numbers, javascript only supports floating point numbers not integers. But if your rounding error is in the region 1957.3000000000001 and the most precise calculations you care about are in the region of 1957.0004 then you can get away with millions of calculations before you have to worry about the rounding error significantly affecting your results.

Community
  • 1
  • 1
JimCresswell
  • 164
  • 1
  • 3