Ruby on rails求和数字结果问题?

时间:2017-07-21 20:04:33

标签: ruby-on-rails postgresql sum

我在postgres中有一个数据库,我的表Venta有一个名为" importe"的列。 (numeric)。在rails上使用ruby我在控制台中执行此查询:

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

结果是0.762e3

但预期结果为762

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

1 个答案:

答案 0 :(得分:0)

您可以使用字符串格式:

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