How To Strip Insignificant Zeros From a Float In Rails

by Frank

I had a float attribute in a Rails application and wanted to strip non-significant zeros when displaying it on a page. With the number_with_precision helper from ActionView, that’s a trivial thing:

number_with_precision(
  3.50,
  strip_insignificant_zeros: true,
  precision: 2
)
# => "3.5"
Bookmark this post : These icons link to social bookmarking sites where readers can share and discover new web pages.
  • DZone
  • Reddit
  • del.icio.us
  • Digg
  • Furl
  • Technorati
  • StumbleUpon