This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Java in General and the fly likes How do I remove decimal part of double? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "How do I remove decimal part of double?" Watch "How do I remove decimal part of double?" New topic
Author

How do I remove decimal part of double?

Matt Senecal
Ranch Hand

Joined: Dec 01, 2000
Posts: 255
There's a function that I'm working with that returns a double with a very long decimal part tacked onto it. The units the double represents are so precise that I can just lose the decimal part. I was thinking of using a StringBuffer to parse the double and remove the offending decimal. Anyone know an easier way to do this?



There's nothing more dangerous than a resourceful idiot. ---Dilbert
Cindy Glass
"The Hood"
Sheriff

Joined: Sep 29, 2000
Posts: 8521
Use a DecimalFormat.


"JavaRanch, where the deer and the Certified play" - David O'Meara
Matt Senecal
Ranch Hand

Joined: Dec 01, 2000
Posts: 255
Thanks for the reply. In all honesty, though, that seems to be a lot more complex than my original idea. I'm only dealing with two numbers here, and implementing a DecimalFormat seems to be a bit much.
Originally posted by Cindy Glass:
Use a DecimalFormat.

Paul Stevens
Ranch Hand

Joined: May 17, 2001
Posts: 2823
You could use a Double and the intValue() or longValue() method of Double.
[This message has been edited by Paul Stevens (edited September 28, 2001).]
shilpa kulkarni
Ranch Hand

Joined: Jun 07, 2000
Posts: 87
or, you can use the NumberFormat class, this way u can set the number of digits after decimal that u want.
here is an example :
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
If you just want to drop the fractional part entirely and round to the nearest integer, it's quite simple:
<pre> int intVal = (int) doubleVal;</pre>

[This message has been edited by Jim Yingst (edited September 28, 2001).]


"I'm not back." - Bill Harding, Twister
Peter den Haan
author
Ranch Hand

Joined: Apr 20, 2000
Posts: 3252
And if you want to round differently, it's almost as simple - explore java.lang.Math (e.g. Math.floor() etc).
- Peter
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How do I remove decimal part of double?
 
Similar Threads
Obtaining only a decimal portion of a number.
double precision
Double Value
Decimals not converting to correct fractions
validations problems(decimal value check?)