Ruby 怎么将一个小数 转换成 % 数?

ginchenorlee · April 21, 2013 · Last by small_fish__ replied at September 02, 2013 · 5798 hits

1.to_f /3.to_f

=>0.3333333333333333 怎么将一个 小数转换诚 百分数? 一定是 33% 格式的, 求指教......

ruby 或 rails

用了{number_to_percentage(1.0/4.0) 这个方法 不行...

乘以 100 不就对了!

irb> (0.3333333333333333  * 100).to_i
33

还要 加个 % 号

irb> "%d" % (a*100) + "%"

那就加个百分号嘛

number_to_percentage(1.0*100/4.0, precision: 0)
=> "25%"

You need to Sign in before reply, if you don't have an account, please Sign up first.