Meaningless Drivel is fun!
The moose likes JDBC and Relational Databases and the fly likes better ways to truncate a double Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » JDBC and Relational Databases
Bookmark "better ways to truncate a double" Watch "better ways to truncate a double" New topic

better ways to truncate a double

Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
Gday, Previously I have been trying to find a method easily truncate a double to a fixed number of decimal places, rounding in the traditional grade school style. Tried big decimal classes and all but There was no reaql easy way to get the required end result. What I have implemented and am using (although inefficient) is Multiplying the double by 10^(number of required places + 1), turn into integer,finding the remainder after dividing by 10, adding or subtracting the appropriate amount, turning into a string then manually inserting a decimal point and zeros if required then returning String.toDouble(). This works perfectly but there must be an easier way?. B. Till
I agree. Here's the link:
subject: better ways to truncate a double
It's not a secret anymore!