On May 16, 2009, at 7:06 AM, Max Williams wrote:

> Thanks rob!
>
> I combined that with the rails helper 'number_with_delimiter, and it
> works great:
>
> def number_to_n_significant_digits(number, n = 3)
>   ("%f"%[("%.#{n}g"%number).to_f]).sub(/\.?0*\z/,'')
> end
>
> #in my other method
> number_with_delimiter(number_to_n_significant_digits(number))
>
> I don't think the 0.0000008 instead of 0.000000766 case will be a
> problem.

Okey-dokey! "soft" requirement for the win!
>
>
> My next quest is to work out what's going on in your code snippet.
> I can see that the sub at the end removes trailing zeros, right?

Yes, in English it says:

If there is a series of 0's (0*) at the end of the string (\z)  
possibly preceeded by a decimal point (\.?), replace all that with  
nothing (i.e., delete it).

>
>
> And the first part looks like some oldschool c-style string
> substitution, time to pull out the pickaxe :)
>
> thanks again!
> max


String#% will direct you to Kernel#sprintf, but you're already on the  
right track.

-Rob

Rob Biedenharn		http://agileconsultingllc.com
Rob / AgileConsultingLLC.com