Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • 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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic