aspose file tools*
The moose likes Beginning Java and the fly likes how to check if a double is exact multiple of an increment value Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "how to check if a double is exact multiple of an increment value" Watch "how to check if a double is exact multiple of an increment value" New topic
Author

how to check if a double is exact multiple of an increment value

premkumar bhaskal
Greenhorn

Joined: Mar 16, 2011
Posts: 2

Hi,
I want to check if a given double number is multiple of any given increment value.
Eg. i want to check if number 2.3 is a multiple of 0.01.
For this i wrote the following code.
.
but i am getting the below result:
the result is 229.99999999999997
the number 2.3 is not exactly divisble

Can someone suggest a better and robust way to check the multiplicity of double values, which will work for any double values.

Thanks & Regards
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19718
    
  20

Use BigDecimal, or use int / long to represent the double times X (with X being some power of 10). float and double simply are never accurate enough; see item #20 of our beginners FAQ for more info.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: how to check if a double is exact multiple of an increment value