small medium large xlarge

Sueatsem_small_pragsmall
07 Apr 2010, 06:18
Sue Petersen (4 posts)

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 in Reston 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 (quite a few of them from you guys.)

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.

*<h1>Listing money</h1>

*<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, and a true bug in RAILS (or Ruby). 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. And I’m sorry about the wonky formatting. It’s late and I’m having trouble working with the blog commands.)

Sue Petersen suep@cowgirlcoder.com http://www.cowgirlcoder.com

Samr_small_pragsmall
08 Apr 2010, 14:38
Sam Ruby (634 posts)

I can’t readily reproduce this problem:

$ ruby -v
ruby 1.8.7 (2008-06-20 patchlevel 22) [i686-darwin9]
$ ruby script/console 
Loading development environment (Rails 2.3.5)
>> include ActionView::Helpers::NumberHelper
=> Object
>> number_to_currency(10542.28)
=> "$10,542.28"
Sueatsem_small_pragsmall
08 Apr 2010, 21:12
Sue Petersen (4 posts)

You’re running an earlier version of Ruby than I am. Which makes me think it’s a problem in my version. I don’t have the experience with Ruby to know where to go from here. Somebody on StackOverflow suggested I write my own routine (and gave me some code samples to play with.) Thoughts?

Can you (or somebody here) take a look at their code suggestions and tell me what you think? I don’t particularly mind using my own routine, at least until Ruby gets fixed, but I don’t have the experience with this language to be able to judge the code quality.

http://stackoverflow.com/questions/2597212/ruby-number-to-currency-displays-a-totally-wrong-number

Thanks!

Samr_small_pragsmall
09 Apr 2010, 11:17
Sam Ruby (634 posts)

I’ve tried a variety of versions of Ruby and Rails. For example:

$ ruby -v
ruby 1.8.7 (2010-01-10 patchlevel 249) [i686-linux]
$ ruby script/console 
Loading development environment (Rails 2.3.6)
ruby-1.8.7-p249 > include ActionView::Helpers::NumberHelper
 => Object 
ruby-1.8.7-p249 > number_to_currency(10542.28)
 => "$10,542.28" 

$ ruby -v
ruby 1.9.2dev (2010-04-09 trunk 27265) [i686-linux]
$ ruby script/console 
Loading development environment (Rails 2.3.6)
ruby-1.9.2-r27265 > include ActionView::Helpers::NumberHelper
 => Object 
ruby-1.9.2-r27265 > number_to_currency(10542.28)
 => "$10,542.28" 

Without seeing the problem myself, I am reluctant to make a recommendation. The suggestions provided look reasonable, but they closely match how number_to_precision[1] is implemented (click show source to see for yourself), so they may suffer the same issue.

[1] http://api.rubyonrails.org/classes/ActionView/Helpers/NumberHelper.html#M001686

Sueatsem_small_pragsmall
10 Apr 2010, 10:25
Sue Petersen (4 posts)

I’ve been hammering at this for a couple of days. You’re right, the suggestions on stackoverflow produced the same bug, it’s obviously at a lower level. I managed to get a newer version of Ruby installed on my MacBook and it does NOT have the problem. (ruby 1.8.7 (2010-01-10 patchlevel 249)) So now I’m trying to figure out how to install that version of Ruby into the system on Snow Leopard.

I’m using the rvm gem, which seems quite nice, but it’s a bear to integrate into NetBeans. And since my system still uses the old, buggy Rails, I hit the bug whenever I call the program from a shortcut on my desktop. And, of course, the Mac mini I’ve been using as a test server decided to crash tonight, while I was trying to upgrade the Ruby version on it. (The drive ate my Snow Leopard disk.) I’m so frustrated at the moment, I could eat nails. Iron nails.

You must be logged in to comment