21

Is there a way to print a decimal as a percentage, so only the two digits after the period?

My decimals will always be between 1 and 0, so I suppose it would work to call number.round(2) starting with the third character, but I can't find the syntax for that anywhere.

To clarify, I want the number to be stored as a full decimal, but printed as a percentage.

Joel
  • 4,503
  • 1
  • 27
  • 41

2 Answers2

31

You are probably going to want to use the number_to_percentage method. From the documentation, here are some examples of how to use it:

number_to_percentage(100)                                        # => 100.000%
number_to_percentage("98")                                       # => 98.000%
number_to_percentage(100, precision: 0)                          # => 100%
number_to_percentage(1000, delimiter: '.', separator: ',')       # => 1.000,000%
number_to_percentage(302.24398923423, precision: 5)              # => 302.24399%
number_to_percentage(1000, locale: :fr)                          # => 1 000,000%
number_to_percentage("98a")                                      # => 98a%
number_to_percentage(100, format: "%n  %")                       # => 100  %

Options:

:locale - Sets the locale to be used for formatting (defaults to current locale).
:precision - Sets the precision of the number (defaults to 3).
:significant - If true, precision will be the # of significant_digits. If false, the # of fractional digits (defaults to false).
:separator - Sets the separator between the fractional and integer digits (defaults to “.”).
:delimiter - Sets the thousands delimiter (defaults to “”).
:strip_insignificant_zeros - If true removes insignificant zeros after the decimal separator (defaults to false).
:format - Specifies the format of the percentage string The number field is %n (defaults to “%n%”).

Or you can write some ruby like the following:

 class Numeric
   def percent_of(n)
    self.to_f / n.to_f * 100.0
   end
 end

p (1).percent_of(10)    # => 10.0  (%)
p (200).percent_of(100) # => 200.0 (%)
p (0.5).percent_of(20)  # => 2.5   (%)
Jon Schneider
  • 25,758
  • 23
  • 142
  • 170
Joel
  • 4,503
  • 1
  • 27
  • 41
  • 5
    I am just curious, if you are going to copy and paste everything from the rails documentation why not just provide a link to it ? or is this the preferred way of answering questions on Stackoverflow ? – Alireza Oct 20 '14 at 08:42
  • 10
    @Ali Actually, this is mostly the best way to do this. Whereas this answer really should attribute the Rails documentation with a link, providing merely a link is not best practice for StackOverflow. The link could become broken with time or the documentation might change rendering the answer less useful. So, yes, it would be nice if there was a link provided but this answer is better than just a link to documentation. – Charles Caldwell Oct 20 '14 at 22:32
  • 5
    For the lazy, you'll need `include ActionView::Helpers::NumberHelper` – Warpling Feb 24 '16 at 18:13
7

You can use number_to_percentage helper to print number as percentage in your view. if your number is between 0 and 1 then you can achieve it with:

number_to_percentage(@number * 100, precision: 0) 

see the documentation

Alireza
  • 2,641
  • 17
  • 18