How to get the fractional part of this number without so much precision?

Hey guys,

I have a number type double and want to split it up into its integer and fractional parts. For example, if I have 3.45 i want a variable that has 3 and another one that has 45.

I have an idea of how to do it, so I started writing. However, I get the following:

Code :

double num = 3.45;
double integerPart = Math.floor(num);
System.out.println(integerPart);
double fractionalPart = num - integerPart;
System.out.println(fractionalPart);

Output:

Quote:

3.0

0.4500000000000002

When I subtract 3.45 - 3, I get 0.4500000000000002 instead of just 0.45. How can I get that 0.45? Am I doing something wrong?

Thank you!

Re: How to get the fractional part of this number without so much precision?

The math is done in binary, it is usually reliable, but it can sometimes be a bit off (lol, perhaps a play on words there). As far as I know, there is not much that can be done about it, other than some intuitive checking on your behalf.

--- Update ---

Here is a good article explaining it better than me: Java theory and practice: Where's your point?

Re: How to get the fractional part of this number without so much precision?

Then is there a way that I can extract the fractional part away from that double? My main objective is, if I have an imported double variable with any value, say 3.45, then somehow get a variable with the value 45 in it (without the 0. in front).

Re: How to get the fractional part of this number without so much precision?

A must read: What Every Computer Scientist Should Know About Floating-Point Arithmetic

Here are some possible solutions:

1. Don't use float/double at all. Instead, use int/long/BigInteger and manually transpose the decimals. This is generally how it's done in the financial world when dealing with money.

2. Use BigDecimal. I'm hesitant to suggest this because there are some results of operations in base 10 which can only be expressed exactly in the limit as the number of bits goes to infinity, which obviously isn't acceptable.

3. Define an arbitrary cutoff point. This is much trickier to do because of the "floating point" part of floating point numbers. There's not really a clear-cut answer for how to determine what the cutoff point is unless you know what range of numbers you are dealing with.

Re: How to get the fractional part of this number without so much precision?

If you want a simple solution for numbers with only a limited number of digits after the dot then you can always use multiplication.

Say, if you already know that your number will never have more then 2 digits after the floating point then you can do:

Code :

int fractionalPart = ((int) (num * 100)) - integerPart;

Which should give you the correct result in pretty much every case for 2 digits.

Re: How to get the fractional part of this number without so much precision?

You can also use a DecimalFormat which can produce a String representation of you value to 2 decimal points. Then parse that value back to a double/float.