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' Thanks in advance! PS - 'Nine Edge Down' refers to the notched edge on 80 column punch cards.

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 ]

JavaBeginnersFaq "Yesterday is history, tomorrow is a mystery, and today is a gift; that's why they call it the present." Eleanor Roosevelt

Gregory N. Mirsky
Greenhorn

Joined: Feb 05, 2002
Posts: 4

posted

0

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".

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

Joined: Jul 22, 2000
Posts: 9047

10

posted

0

Slight modification of the previous code should work.

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.

"I'm not back." - Bill Harding, Twister

Gregory N. Mirsky
Greenhorn

Joined: Feb 05, 2002
Posts: 4

posted

0

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