开发者

ruby on rails - Two digit precision

ia开发者_开发知识库m working on RoR code, i want to know how to find two digit precision for the numbers

a = 0.7

b = 5

thanks


Always your standard

a = "%.2f" % 0.2
b = "%.2f" % 5

0.20

5.00

Outputs strings though, so you may need ("%.2f" % 0.2).to_i or cast later (a.to_i).


number_with_precision. If you're looking to express them as currency, use number_to_currency.


If you're looking to store those numbers and their precision in the database you should set up decimal fields.

 add_column :table_name, :column_name, :decimal,  :precision => 5, :scale => 2

Options for a decimal field:

  • :precision : maximum number of digits in a value
  • :scale : number of digits after the decimal place

With a decimal field you will never need the helper methods to limit the precision after the decimal place.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜