Search

Loading...

Wednesday, April 23, 2008

To get the fractional portion of a number in Java

Problem:How do to get the fractional portion of a number in Java?
Suppose we have a number 4.56.
We need to get 0.56(fractional part) of it.
Solution:
double fraction(double d) {
if (d >= 0)
return d-Math.floor(d);
return d-Math.ceil(d);
}

Thanks to this link:
http://forum.java.sun.com/thread.jspa?threadID=752735&messageID=4301382

1 comment:

Pravin More said...

it is really good and simple solution Thanks :)