Win a copy of Functional Reactive Programming this week in the Other Languages forum!

# Obtaining only a decimal portion of a number.

Gregory N. Mirsky
Greenhorn
Posts: 4
It's been quite a while since I used JAVA, I'm finishing undeprecating alot of the code I wrote years ago (Can it be that long?) I need a method that will only return the decimal portion of a number (i.e. fx(123.456) would return .456) but I can't find a built in method to do so. I know other languages have this capability and I could do: x = 123.456; y = x - abs(round(x)); but there has to be a better way. I've searched java.lang and java.math but I can't find a suitable method. I must be going senile -- what do you expect from a 'Nine Edge Down Programmer'
PS - 'Nine Edge Down' refers to the notched edge on 80 column punch cards.

Dirk Schreckmann
Sheriff
Posts: 7023
Hello,
I don't know if this is of any benefit to you...
To get the decimal part you could do this:
double original = 1.432d;
double decimal = original % 1d;
Note that due to the way that decimals are actually thought of, this might kill any precision you were after. The decimal part of 1.432 is actually thought of as .43199999999999994 once calculations are begun. So, from above, original - decimal would produce the number 1.0 and not 1.00000000000000006.
Good Luck,
-Dirk Schreckmann
[ February 05, 2002: Message edited by: Dirk Schreckmann ]

Marilyn de Queiroz
Sheriff
Posts: 9067
12

Could modulo be what you're looking for?

Gregory N. Mirsky
Greenhorn
Posts: 4
Close but not it. I've already chased those options down and tried to make them work. No Luck.
I need what is on the right side of the decimal and not what is on the left. According to the calculation the left side (the whole number part) can be anything 1, 2, 300, 45000, etc. the right part can be any precision but for sanity sake I'll call it quits after 6 digits. I was thinking of using java.text to format the number and substring the decimal part but there must be a better way. I don't like mixing number with strings, ecspecially in calculations, it is just an invitation for bugs -- "been there, done that and have the scars to prove it".

Cindy Glass
"The Hood"
Sheriff
Posts: 8521
Could you use something like:
double d = 123.456;
double justDecimal = d - Math.floor(d);
Of course again, you will have that inprecision that creeps in when dealing with doubles.
Plus negatives might not give you the result that you are looking for.

Marilyn de Queiroz
Sheriff
Posts: 9067
12

Slight modification of the previous code should work.

Jamie Robertson
Ranch Hand
Posts: 1879

or if you don't need the decimal point included in the decimal String you can use
String decimal = doubleValue.substring(doubleValue.indexOf('.')+1);

Jamie

Jim Yingst
Wanderer
Sheriff
Posts: 18671
Well, how should -4.7 be treated? Do you want 0.7, -0.7, 0.3, or -0.3? I'll guess the first:

Contrary to previous comments, you don't actually have to worry about introducing imprecisions in double in this case (or in any other code examples given here, really). The inherent roundoff errors in double only occur when calculating new fractional values - since none of the fractional values change in these methods, you won't introduce any errors that weren't already present in the double before your method got there.

Gregory N. Mirsky
Greenhorn
Posts: 4
To all, Thank You!
return Math.abs(nX - (int) nX);
did what I required.
Thanks for clearing the cob webs out of my head!