Ruby number_to_currency displays a totally wrong number!

Posted by SueP on Stack Overflow See other posts from Stack Overflow or by SueP
Published on 2010-04-08T02:32:20Z Indexed on 2010/04/08 4:13 UTC
Read the original article Hit count: 299

Filed under:
|

Greetings! I’m an old Delphi programmer making the leap to the Mac, Ruby, Rails, and web programming in general. I’m signed up for the Advanced Rails workshop at the end of the month. In the meantime, I’ve been working on porting a mission-critical (of course) app from Delphi to RAILS. It feels like I’ve spent most of the past year with my head buried in a book or podcast.

Right now, I’ve hit a major issue and I’m tearing my hair out. I literally don’t know where to go with this, I desperately don’t want to deploy with this bug, and I’m feeling a bit frantic. (The company database is currently running on an ancient XP box that’s looking rustier by the day.)

So, I set up a test database that shows the problem. I’m running:

 OS/X 10.6.3
 Rails 2.3.5
 ruby 1.8.7 (2009-06-08 patchlevel 173) [universal-darwin10.0]
 MySQL 5.1.38-log via socket
 MySQL Client Version 5.1.8

ActiveRecord::Schema.define(:version => 20100406222528) do

     create_table “money”, :force => true do |t|
     t.decimal “amount_due”, :precision => 10, :scale => 2, :default => 0.0
     t.decimal “balance”, :precision => 10, :scale => 2, :default => 0.0
     t.text “memofield”
     t.datetime “created_at”
     t.datetime “updated_at”
     end

The index view is right out of the generator, slightly modified to add the formatting that's breaking on me.

Listing money
<table>

     <tr>
     <th>Amount</th>
     <th>Amount to_s </th>
     <th>Balance to $</th>
     <th>Balance with_precision </th>
     <th>Memofield</th>
     </tr>

<% @money.each do |money| %>

     <tr>
     <td><%=h money.amount_due %></td>
     <td><%=h money.amount_due.to_s(‘F’) %></td>
     <td><%=h number_to_currency(money.balance) %></td>
     <td><%=h number_with_precision(money.balance, :precision => 2) %></td>
     <td><%=h money.memofield %></td>
     <td><%= link_to ‘Show’, money %></td>
     <td><%= link_to ‘Edit’, edit_money_path(money) %></td>
     <td><%= link_to ‘Destroy’, money, :confirm => ‘Are you sure?’, :method => :delete %></td>
     </tr> *<% end %> *</table>

<%= link_to ‘New money’, new_money_path %>

This seemed to work pretty well. Then I started testing with production data and hit a major problem with number_to_currency.

The number in the database is: 10542.28, I verified it with the MySQL Query Browser. RAILS will display this as 10542.28 unless I call number_to_currency, then that number is displayed as: $15422.80

The error seems to happen with any number between 10,000.00 and 10,999.99 So far, I haven’t seen it outside of that range, but I obviously haven’t tested everything.

I guess my workaround is to remove number_to_currency, but that leaves the views looking really sloppy and unprofessional. The formatting is messed up, things don’t line up properly and I can’t force the display to 2 decimal places.

I’m seriously hoping there is an easy fix for this. I can’t imagine this being a widespread problem. It would affect so many people that someone would have fixed it! But I don’t know where to go from here.

I’d desperately like some help.

(Later – number_with_precision fails the same way number_to_currency does.)

Sue Petersen

© Stack Overflow or respective owner

Related posts about ruby

Related posts about rails