From: Paul Lutus Date: 2006-12-06T07:30:07+09:00 Subject: Re: number format Bernard Kenik wrote: > You can use the number.round method, but this doesn't take a number of > decimal places. > > To round 1.234 to 1.23 use this code: > x = 1.234 > x = x * 100 # -> 123.4 > x = x.round -> 123 > x = x / 100.0 => 1.23 > > At least, that's the best method I can find from the Ruby docs... Second reply to the second copy of this identical post. This is not a good idea. The problem is that Ruby's numbers are stored internally in binary form, and this multiply -- truncate -- divide process will not work as intended for a lot of numbers. It is better to retain the full resolution of binary numbers internally, and simply print the number with the desired number of decimal places: -------------------------------- #!/usr/bin/ruby -w 0.upto(10) do n = rand() * 100 printf("%5.02f\n",n) end -------------------------------- Output: 18.64 61.11 58.18 61.80 93.98 50.86 55.28 44.93 87.23 1.38 11.63 -- Paul Lutus http://www.arachnoid.com