轨道上的红宝石求和数字结果问题?



我在postgres中有一个db,我的表Venta有一个名为"importe"(numeric(的列。使用 Ruby on rails,我在控制台中执行此查询:

Venta.where(date:"2017-03-03").sum(:importe) 

结果是0.762e3

但预期的结果是762.

如何解决此问题? 请记住,列类型是numeric,我无法更改它。提前谢谢。

您可以使用字符串格式:

venta = Venta.where(date:"2017-03-03").sum(:importe)
= > 0.762e3
print_venta = '%d' % venta
=> "762"

最常见的类型字符是:

Field  |  Format
---------+--------------------------------------------------------------
d,i,u  | Convert argument as a decimal number.
f      | Convert floating point argument as [-]ddd.dddddd,
| where the precision specifies the number of digits after
| the decimal point.

源: http://ruby-doc.org/core-1.9.3/Kernel.html#method-i-sprintf

最新更新