Rounding floating-point numbers to 4 decimal points

Posted by Himadri on Stack Overflow See other posts from Stack Overflow or by Himadri
Published on 2010-05-14T05:55:34Z Indexed on 2010/05/14 6:04 UTC
Read the original article Hit count: 296

Filed under:
|

I have two decimal numbers. I want those number to be same upto 4 decimal points without rounding. If numbers are different I want 2nd number to be replaced by 1st. What if condition should I write?

Eg,
1.

num1 = 0.94618976  
num2 = 0.94620239

If we round these numbers upto 4 decimal then we get 0.9462 same number, but I don't want to round these numbers.

2.

num1 = 0.94620239  
num2 = 0.94639125  

The one way I found is take absolute difference of both numbers say diff and then check the value. My problem is of checking the range of diff.

Thank You.

© Stack Overflow or respective owner

Related posts about rounding

Related posts about floating-point