Rails: How to Print a Decimal as a Percent

Rails: How to print a decimal as a percent?

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 (%)

How to convert float to percentage in Ruby

To convert a float to a percentage, multiply the float by 100. If you need to output it with the % sign, use string interpolation inside a string.

Example:

percentage_value  = 0.02 * 100
puts "#{percentage_value}%"

2.0%

{edited}

Format number with percent sign

I didn’t test, but I feel you should pass 0.75 instead: (number/100.0).

How to read percentage as decimal number?

I'm trying to find the amount from a percentage that a user inputs

If you retrieve the input via gets, you typically convert it to a numeric value first, e.g.

percentage = gets.to_i
#=> 15

Ruby is not aware that this 15 is a percentage. And since there's no Percentage class, you have to convert it into one of the existing numeric classes.

15% is equal to the fraction 15/100, the ratio 15:100, or the decimal number 0.15.

If you want the number as a (maybe inexact) Float, you can divide it by 100 via fdiv:

15.fdiv(100)
#=> 0.15

If you prefer a Rational you can use quo: (it might also return an Integer)

15.quo(100)
#=> (3/20)

Or maybe BigDecimal for an arbitrary-precision decimal number:

require 'bigdecimal'

BigDecimal(15) / 100
#=> 0.15e0

BigDecimal also accepts strings, so you could pass the input without prior conversion:

input = gets
BigDecimal(input) / 100
#=> 0.15e0

How can I strip the decimal point and everything after it in a float?

You can use the to_integer method in Ruby to strip the integer part of the float:

>123.434343.to_i
>123

http://ruby-doc.org/core-2.2.0/Integer.html

Just found it already answered in a similar question:

Split float into integer and decimals in Ruby

Money format when printing number

First you need to fix your math. 75% is equal to 75/100 so you want

total_percent_of = number * percent / 100.0

Next you need a format string to ensure that total_percent_of is always printed with the correct number of decimals:

sprintf " #{percent}%% of #{number} is %.2f", total_percent_of

(you need the %% after percent because percent signs have special meaning to sprintf). See the documentation for more information about string formatting.

Money format when printing number

First you need to fix your math. 75% is equal to 75/100 so you want

total_percent_of = number * percent / 100.0

Next you need a format string to ensure that total_percent_of is always printed with the correct number of decimals:

sprintf " #{percent}%% of #{number} is %.2f", total_percent_of

(you need the %% after percent because percent signs have special meaning to sprintf). See the documentation for more information about string formatting.

How do you round a float to 2 decimal places in JRuby?

Float#round can take a parameter in Ruby 1.9, not in Ruby 1.8. JRuby defaults to 1.8, but it is capable of running in 1.9 mode.

How to calculate percentages with rounding

OK, I think we can separate two answers here. First of all, you have to pay attention with rounding and truncating values. It could be dangerous in terms of inconsistent information. Anyways, based on your case, here it is.

  • If you want to display only integers and ignore the decimal part of the number, you can use either ceil or floor

    0.47939999999999994.ceil 
    #=> 1

    0.47939999999999994.floor
    #=> 0
  • If you want to round your decimal, you can use round passing the precision desired:

    0.47939999999999994.round
    #=> 0

    0.47939999999999994.round(1)
    #=> 0.5

    0.47939999999999994.round(2)
    #=> 0.48

    0.47939999999999994.round(3)
    #=> 0.479

    0.47939999999999994.round(4)
    #=> 0.4794

Your method should look like the following (using round)

def self.percent
((Order.revenue.to_f.round(2) / Order.goal.to_f.round(2)) * 100).round(2)
end

I hope it helps !



Related Topics



Leave a reply



Submit