如何在jruby中将浮点数四舍五入到小数点后两位

时间:2021-12-31 11:13:38

JRuby 1.6.x. How do you round a float to decimal places in jruby.

JRuby 1.6.x。如何在jruby中将浮点数四舍五入到小数。

number = 1.1164
number.round(2)

The above shows the following error
wrong number of arguments (1 for 0)

How do I round this to 2 decimal places?

我怎么把它四舍五入到小数点后两位呢?

6 个解决方案

#1


75  

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.

Float#round可以使用Ruby 1.9中的参数,而不是Ruby 1.8中的参数。JRuby默认为1.8,但它可以在1.9模式下运行。

#2


242  

(5.65235534).round(2)
#=> 5.65

#3


158  

sprintf('%.2f', number) is a cryptic, but very powerful way of formatting numbers. The result is always a string, but since you're rounding I assume you're doing it for presentation purposes anyway. sprintf can format any number almost any way you like, and lots more.

sprintf(“%。2f',数字)是一种神秘的,但非常强大的数字格式化方法。结果总是一个字符串,但是由于是四舍五入的,所以我认为您这样做是出于表示目的。sprintf几乎可以用任何你喜欢的方式格式化任何数字,还有更多。

Full sprintf documentation: http://www.ruby-doc.org/core-2.0.0/Kernel.html#method-i-sprintf

完整的sprintf文档:http://www.ruby-doc.org/core-2.0.0/Kernel.html # method-i-sprintf

#4


1  

Edit

After getting feedback, It seems the original solution didn't work. That's why updated the answer as one of the suggestions.

得到反馈后,原来的解决方案似乎没有效果。这就是为什么要更新答案作为建议之一。

def float_of_2_decimal(float_n) 
  float_n.to_d.round(2, :truncate).to_f
end

Other answers may work, if you want to have rounded numbers of 2 decimal places. But, If you want to have floating point numbers with first two decimal places without rounding, Those answers won't help.

如果你想要小数点后两位的整数,其他答案也可以。但是,如果你想把浮点数和前两个小数点没有四舍五入,这些答案是没有用的。

So, to get a floating point number with first two decimal places, I used this technique. Doesn't work in some cases

因此,为了得到一个浮点数和前两个小数点,我用了这个技巧。在某些情况下行不通

def float_of_2_decimal(float_n)
  float_n.round(3).to_s[0..3].to_f
end

with 5.666666666666666666666666, it will return 5.66 instead of rounded 5.67. Hope it will help someone

以5.6666666666666666666666,将5.66改为5.66,而不是整数5.67。希望它能帮助某人

#5


-3  

Try this:

试试这个:

module Util
module MyUtil



    def self.redondear_up(suma,cantidad, decimales=0)

        unless suma.present?
            return nil
        end


        if suma>0
            resultado= (suma.to_f/cantidad)
            return resultado.round(decimales)
        end


        return nil


    end

end 
end 

#6


-14  

to truncate a decimal I've used the follow code:

为了截断小数,我使用了以下代码:

<th><%#= sprintf("%0.01f",prom/total) %><!--1dec,aprox-->
    <% if prom == 0 or total == 0 %>
        N.E.
    <% else %>
        <%= Integer((prom/total).to_d*10)*0.1 %><!--1decimal,truncado-->
    <% end %>
        <%#= prom/total %>
</th>

If you want to truncate to 2 decimals, you should use Integr(a*100)*0.01

如果要将小数截断为2,应该使用Integr(a*100)*0.01

#1


75  

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.

Float#round可以使用Ruby 1.9中的参数,而不是Ruby 1.8中的参数。JRuby默认为1.8,但它可以在1.9模式下运行。

#2


242  

(5.65235534).round(2)
#=> 5.65

#3


158  

sprintf('%.2f', number) is a cryptic, but very powerful way of formatting numbers. The result is always a string, but since you're rounding I assume you're doing it for presentation purposes anyway. sprintf can format any number almost any way you like, and lots more.

sprintf(“%。2f',数字)是一种神秘的,但非常强大的数字格式化方法。结果总是一个字符串,但是由于是四舍五入的,所以我认为您这样做是出于表示目的。sprintf几乎可以用任何你喜欢的方式格式化任何数字,还有更多。

Full sprintf documentation: http://www.ruby-doc.org/core-2.0.0/Kernel.html#method-i-sprintf

完整的sprintf文档:http://www.ruby-doc.org/core-2.0.0/Kernel.html # method-i-sprintf

#4


1  

Edit

After getting feedback, It seems the original solution didn't work. That's why updated the answer as one of the suggestions.

得到反馈后,原来的解决方案似乎没有效果。这就是为什么要更新答案作为建议之一。

def float_of_2_decimal(float_n) 
  float_n.to_d.round(2, :truncate).to_f
end

Other answers may work, if you want to have rounded numbers of 2 decimal places. But, If you want to have floating point numbers with first two decimal places without rounding, Those answers won't help.

如果你想要小数点后两位的整数,其他答案也可以。但是,如果你想把浮点数和前两个小数点没有四舍五入,这些答案是没有用的。

So, to get a floating point number with first two decimal places, I used this technique. Doesn't work in some cases

因此,为了得到一个浮点数和前两个小数点,我用了这个技巧。在某些情况下行不通

def float_of_2_decimal(float_n)
  float_n.round(3).to_s[0..3].to_f
end

with 5.666666666666666666666666, it will return 5.66 instead of rounded 5.67. Hope it will help someone

以5.6666666666666666666666,将5.66改为5.66,而不是整数5.67。希望它能帮助某人

#5


-3  

Try this:

试试这个:

module Util
module MyUtil



    def self.redondear_up(suma,cantidad, decimales=0)

        unless suma.present?
            return nil
        end


        if suma>0
            resultado= (suma.to_f/cantidad)
            return resultado.round(decimales)
        end


        return nil


    end

end 
end 

#6


-14  

to truncate a decimal I've used the follow code:

为了截断小数,我使用了以下代码:

<th><%#= sprintf("%0.01f",prom/total) %><!--1dec,aprox-->
    <% if prom == 0 or total == 0 %>
        N.E.
    <% else %>
        <%= Integer((prom/total).to_d*10)*0.1 %><!--1decimal,truncado-->
    <% end %>
        <%#= prom/total %>
</th>

If you want to truncate to 2 decimals, you should use Integr(a*100)*0.01

如果要将小数截断为2,应该使用Integr(a*100)*0.01