当前位置: 动力学知识库 > 问答 > 编程问答 >

mysql - SQL Query, Rails view, remove decimal place

问题描述:

I currently have this in my controller:

@softwares = Software.find_by_sql

(

"SELECT s.id, s.vendor, s.title, s.edition, SUM(l.amount) AS amount FROM softwares s

LEFT JOIN licenses l ON s.id=l.software_id GROUP BY s.vendor, s.title, s.edition"

)

Which does what it's supposed to, but the number comes up as 7.0 or 6.0 instead of just 7 or 6.. how do i remove the decimal? In my view i literally just have <%= l.amount %>

网友答案:

It comes up as a decimal because l.amount is a decimal

try

 "SELECT s.id, s.vendor, s.title, s.edition, CAST(SUM(l.amount) as UNSIGNED) AS amount 
  FROM softwares s 
  LEFT JOIN licenses l ON s.id=l.software_id GROUP BY s.vendor, s.title, s.edition"
网友答案:

I'd rather put control over presentation to view. Instead of

<%= l.amount %>

put:

<%= number_to_currency l.amount, :precision => 0 %>

This helper method is described here

On a side note, it may be a better design to put that SQL into model. Controller then does something like @softwares = Software.all_with_licenses_amounts

分享给朋友:
您可能感兴趣的文章:
随机阅读: