File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
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 Customer Requirements for Developers this week in the Jobs Discussion 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

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"

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

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
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:
subject: How do I remove decimal part of double?
It's not a secret anymore!