Number Formatting in Rails
Numbers are of different formats, according to their uses like phone numbers, percentage, currency etc. In Rails
NumberHelper fulfills this requirement by providing methods for formatting numbers into different forms. Few methods are:
(a) number_to_currency : - It formats number like currency.
number_to_currency(5332923729.60) # => $5,332,923,729.60
(b) number_with_precision : - It formats a number to a specified level of precision, by defaults its 3.
number_with_precision(5332.5463) # => 5332.546
number_with_precision(5332.5463, 2) # => 5332.54
(c) number_with_delimiter : - It formats a number by grouping into thousands using a delimiter.
number_with_delimiter(5332923729) # => 5,332,923,729
(d) number_to_phone : - It formats number into US phone number standards.
number_to_phone(9874561230) # => 987-456-1230
(e) number_to_percentage : - It formats number as percentage string.
number_to_percentage(95, precision: 0) # => 95%
(f) number_to_human_size : - It formats bytes into more human readable format by coverting them to KBs or MBs (i.e. larger units).
number_to_human_size(7800) # => 7.8 KB
number_to_human_size(87945140) # => 87.9 MB
Hope you liked this blog. For any queries please feel free to comment.
0 Comment(s)