Rails 3视图 - 向上舍入

时间:2012-05-01 21:48:53

标签: ruby-on-rails-3

我有一个Rails视图来显示PERIOD和GENERAL LEDGER的网格,我想显示累计总数。但是,在下面的情况中,“TOTAL”列中的@cumulative_total会向上舍入到最接近的美元,因此即使它们在general_ledger列中正确显示,也不显示任何分数。我有什么想法吗?

<% @cumulative_total = 0 %>
<div id="gl_crosstab">
  <table>
    <tr>
      <th>Period</th>
      <% @general_ledgers.each do |g| %>
        <th><%= g.general_ledger_number %></th>
      <% end %>
      <th>Total</th>
      <th>% Expended</th>
    </tr>
    <% @expected_billings.group_by(&:period_id).each do |eb| %>
      <tr>
        <td><%= eb[1].first.period.pe_number %></td>
        <% eb[1].each do|p| %>
          <td><%= number_to_currency(p.expected_amount) %></td>
        <% end %>
        <td>
          <% @cumulative_total = @cumulative_total + eb[1].inject(0){|sum,billing| sum+billing.expected_amount.to_i} %>
          <%=  number_to_currency( @cumulative_total ) %> </td>
        <td><%= number_to_currency((@cumulative_total/@sla.project_total)*100) %> % </td>
      </tr>
    <% end %>
    <tr>
      <td><b>Total Budget</td>
      <% @total_expected_billings.each do |teb| %>
        <td><b><%= number_to_currency(teb[1].inject(0){|sum,billing| sum+billing.expected_amount.to_i}) %></td>
      <% end %>
      <td><b><%= number_to_currency(@expected_billings.inject(0){|sum,billing| sum+billing.expected_amount.to_i}) %> </td>
      <td><b><%= number_to_currency((@expected_billings.inject(0){|sum,billing| sum+billing.expected_amount.to_i}/@sla.project_total)*100) %> % </b></td>
    </tr>
  </table>
</div>

1 个答案:

答案 0 :(得分:0)

我认为这是因为您在显示时将结算金额转换为“整数”:

@expected_billings.inject(0){|sum,billing| sum+billing.expected_amount.to_i

如果你改变了

billing.expected_amount.to_i  

billing.expected_amount.to_f  

它应该有用。