create table test_tables (
id number(38) primary key,
name varchar2(50),
age number(5),
gender varchar2(10),
point number(5,2)
);
insert into test_tables values (1,'name1',20,'male',80.5);
insert into test_tables values (2,'name2',21,'female',60.5);
insert into test_tables values (3,'name3',23,'male',90.5);
insert into test_tables values (4,'name4',19,'male',79.5);
insert into test_tables values (5,'name5',18,'female',80.5);
TestTable.sum(:point)的结果是391,
为什么不是 391.5?
我检查了 rdoc,有一些描述:
The value is returned with the same data type of the column
但在我的例子中,“点”的类型是数字 (5,2)。
这不是花车吗?
最佳答案
我不是特别熟悉 SQLite,但我认为您应该在创建表语句中使用 DECIMAL 类型。更多详情:
Active Record SQLite Type Mapping
SQLite Types
小心this issue尽管。似乎 sqlite3 ruby 驱动程序坚持为 DECIMAL 查询结果创建 Float ruby 对象而不是 BigDecimal。
关于ruby-on-rails - 为什么 Module.sum( :field) is integer?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4757046/