DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Snippets has posted 5883 posts at DZone. View Full User Profile

Helper To Format Numbers Using Scientific Notation

08.23.2005
| 8751 views |
  • submit to reddit
        For those of us who can't remember the formatting codes

def number_to_scientific(num,precision=3)
	"%.#{precision}e" % num
end

number_to_scientific(10000) => 1.000e004
in some cases it might be better to use the 'g' code instead of the 'e' code.