简体   繁体   English

如何通过Rails C使用t.decimal精度和小数位数填充sql db

[英]How to populate a sql db with t.decimal precision and scale via rails c

t.decimal  "eur",        precision: 8,  scale: 2

This is my Row in the Database table (name is wallet). 这是我在数据库表中的行(名称是钱包)。 How can I populate it with data in the rails console? 如何在Rails控制台中填充数据?

I have tried 我努力了

wallet.eur=10 Ok this one is to easy and doesnt work. wallet.eur=10好的,这很简单,不起作用。

wallet.eur=BigDecimal.new('154.00') This one throws something like => #<BigDecimal:56cadb8,'0.154E3',9(18)> But i cant save it and will stay at 0.0. wallet.eur=BigDecimal.new('154.00')这个抛出类似=> #<BigDecimal:56cadb8,'0.154E3',9(18)>但我无法保存,它将保持在0.0。

Any Idea or suggestion? 有任何想法或建议吗?

If dealing with currency maybe money-rails gem might be worthwhile? 如果与货币打交道,也许赚钱的宝石可能值得?

Or something similar just using an integer and storing it in pence/cents and creating helpers to display it. 或者类似的事情,只是使用一个整数并将其存储在便士/分中,并创建帮助程序来显示它。

Plays much nicer than using decimal or worse float. 比使用十进制或更差的浮点数要好得多。

Use the built-in rails facilities, check the NumberHelper documentation for examples. 使用内置的滑轨工具,请查看NumberHelper文档以获取示例。

Use this code to add the migration: 使用以下代码添加迁移:

add_column :items, :price, :decimal, :precision => 8, :scale => 2

and then use this method: 然后使用此方法:

number_to_currency(price, :unit => "€")

If you've got an NoMethodError add this line: 如果您遇到NoMethodError,请添加以下行:

include ActionView::Helpers::NumberHelper

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 SQL小数类型-精度和小数位数差异 - SQL Decimal type - Precision and Scale discrepancy SQL Server:十进制精度/比例正在产生奇怪的结果 - SQL Server : Decimal Precision/Scale are yielding strange results sql - 计算平均值时的结果小数精度和小数位数 - sql - result decimal precision and scale when calculating average SQL 服务器保留十进制小数位和精度,以便对非常大的数字进行运算 - SQL Server retain decimal scale and precision for operations on very lage numbers “减少”sql十进制数据类型到最低精度和基于值的缩放 - “Reduce” sql decimal data type to least precision and scale based on value 查询 DECIMAL 列的精度和小数位数 - Query the Precision and Scale of a DECIMAL column 如何使用 phpMyAdmin 在 MySQL 中指定小数精度和小数位数 - How to specify decimal precision and scale number in MySQL using phpMyAdmin 如何从 pandas dataframe 列返回最大十进制“精度”和“比例”? - How to return the maximum decimal "precision" and "scale" from pandas dataframe column? MySQL-如何使用存储的精度/小数位数转换为十进制? - MySQL - How to convert number to decimal using stored precision/scale? DB2 SQL 时间数学十进制精度不可见 - DB2 SQL Time Math decimal precision not visible
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM