This week's book giveaway is in the Mac OS forum.
We're giving away four copies of a choice of "Take Control of Upgrading to Yosemite" or "Take Control of Automating Your Mac" and have Joe Kissell on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Obtaining only a decimal portion of a number. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Obtaining only a decimal portion of a number." Watch "Obtaining only a decimal portion of a number." New topic
Author

Obtaining only a decimal portion of a number.

Gregory N. Mirsky
Greenhorn

Joined: Feb 05, 2002
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'
Thanks in advance!
PS - 'Nine Edge Down' refers to the notched edge on 80 column punch cards.
Dirk Schreckmann
Sheriff

Joined: Dec 10, 2001
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 ]

[How To Ask Good Questions] [JavaRanch FAQ Wiki] [JavaRanch Radio]
Marilyn de Queiroz
Sheriff

Joined: Jul 22, 2000
Posts: 9046
    
  10


Could modulo be what you're looking for?


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

Joined: Sep 29, 2000
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

Joined: Jul 22, 2000
Posts: 9046
    
  10

Slight modification of the previous code should work.
Jamie Robertson
Ranch Hand

Joined: Jul 09, 2001
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

Joined: Jan 30, 2000
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.


"I'm not back." - Bill Harding, Twister
Gregory N. Mirsky
Greenhorn

Joined: Feb 05, 2002
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!
 
GeeCON Prague 2014
 
subject: Obtaining only a decimal portion of a number.