Win a copy of Kotlin in Action this week in the Kotlin forum!
programming forums Java Java JSRs Mobile Certification Databases Caching Books Engineering Languages Frameworks Products This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
Sheriffs:
Saloon Keepers:
Bartenders:

# How do I remove decimal part of double?

Matt Senecal
Ranch Hand
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?

Cindy Glass
"The Hood"
Sheriff
Posts: 8521
Use a DecimalFormat.

Matt Senecal
Ranch Hand
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
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
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
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).]

Peter den Haan
author
Ranch Hand
Posts: 3252
And if you want to round differently, it's almost as simple - explore java.lang.Math (e.g. Math.floor() etc).
- Peter

 Did you see how Paul cut 87% off of his electric heat bill with 82 watts of micro heaters?