带百分号的格式数字
Format number with percent sign
如何在电子表格中添加百分比格式。
在 Ruby 中,合乎逻辑的方法是这样的:
number.to_s + "%"
但是,我想避免表示 number as a string,并且我一直在尝试将 number_format
与电子表格一起使用 gem。
我试过类似的东西:
percent_format = Spreadsheet::Format.new :number_format => '###000 %'
percent_format = Spreadsheet::Format.new :number_format => '#0 %'
导致(分别):
7500%
7500%
格式上的数字是75,所以它就像百分号也代表“00”。
我也在 Gems Github 问题部分问过:https://github.com/zdavatz/spreadsheet/issues/212
如果您正在使用 rails,您可以使用 proper number helper
或者您可以尝试通过 Ruby 格式化程序来完成:
'%.2f%' % 1.23456 =>
"1.23%"
如果您只需要一个没有百分号的四舍五入的数字:
33.22212323.round(2) => 33.22
33.round(2) => 33.0
我没有测试,但我觉得你应该通过 0.75:(number/100.0)
.
如何在电子表格中添加百分比格式。
在 Ruby 中,合乎逻辑的方法是这样的:
number.to_s + "%"
但是,我想避免表示 number as a string,并且我一直在尝试将 number_format
与电子表格一起使用 gem。
我试过类似的东西:
percent_format = Spreadsheet::Format.new :number_format => '###000 %'
percent_format = Spreadsheet::Format.new :number_format => '#0 %'
导致(分别):
7500%
7500%
格式上的数字是75,所以它就像百分号也代表“00”。
我也在 Gems Github 问题部分问过:https://github.com/zdavatz/spreadsheet/issues/212
如果您正在使用 rails,您可以使用 proper number helper
或者您可以尝试通过 Ruby 格式化程序来完成:
'%.2f%' % 1.23456 =>
"1.23%"
如果您只需要一个没有百分号的四舍五入的数字:
33.22212323.round(2) => 33.22
33.round(2) => 33.0
我没有测试,但我觉得你应该通过 0.75:(number/100.0)
.