ruby 你如何在 jruby 中将浮点数四舍五入到两位小数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10459901/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do you round a float to two decimal places in jruby
提问by Sam
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?
如何将其四舍五入到小数点后两位?
采纳答案by steenslag
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 模式下运行。
回答by boulder_ruby
(5.65235534).round(2)
#=> 5.65
回答by Theo
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. sprintfcan format any number almost any way you like, and lots more.
sprintf('%.2f', number)是一种神秘但非常强大的数字格式化方式。结果始终是一个字符串,但由于您是四舍五入,因此我假设您无论如何都是出于演示目的而这样做的。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
回答by Anwar
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.66instead of rounded 5.67. Hope it will help someone
有5.666666666666666666666666,它会返回5.66,而不是圆的5.67。希望它会帮助某人
回答by HalleyRios
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
回答by Ivan Carrasco Quiroz
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

