• Post Reply Bookmark Topic Watch Topic
  • New Topic

better ways to truncate a double

 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Gravity is a harsh mistress. But this tiny ad is pretty easy to deal with:
the new thread boost feature: great for the advertiser and smooth for the coderanch user
https://coderanch.com/t/674455/Thread-Boost-feature
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!