From: Robert Dober Date: 2009-05-16T18:07:06+09:00 Subject: Re: Rounding any number (int or float) to 3 significant figures On Fri, May 15, 2009 at 6:57 PM, Max Williams wrote: > I have a requirement where i need to display (ie convert to a string) > all digits to 3 significant figures.  (not the same as 3 decimal places) > > eg > >    23 => "23" >    26759 => "26,800" >    10.546 => "10.5" >    3332332 => "3,330,000" >    0.766 => "0.766" >    0.00000766 => "0.00000766" > > Can anyone show me a neat way to do this?  I'd have thought that there'd > be a method for it already but i can't find one. > > thanks! > max > -- > Posted via http://www.ruby-forum.com/. > > At frirst sight and testing with the data you have given, the following pretty much seems to do the trick: x.to_s.sub( /(\.0*\d{0,3}).*/, '\1' ) Did I overlook any edge cases? HTH R.