Question Title: Why is the data in a Rails view displaying as 0.0 or 42.0 instead of $42.00?
Summary: How can I get this field to display e.g. $42.00 instead of 0.0 or 42.0? In addition to the display format, it also needs to be in a data type on which I can perform math operations such as sum and average. How do I do that? I can get it to work in Rails Console, but not in the app.
Constraints: I have to fix this in Ruby on Rails, not in Excel.
Question:
I am a relatively inexperienced programmer. At work I am helping develop an app to deal with book purchase requests using Ruby on Rails. I did not create it, but I am updating it.
I am importing data from a csv file. The csv file has a field called “us_list”. When I display it in the view, it shows as 0.0 or 42.0 instead of $42.00. This is because in the csv file the field is a string which looks like ” $42.00 “. Note the whitespace on each side. I need to be able to manipulate the financial data, using sums, averages, etc. I understand I shouldn’t use Floats for currency. I found two possible solutions: number_to_currency and monetize.
Monetize works the best. In my file app/services/slips_csv_import_service.rb, I added:
load '~/.rvm/gems/ruby-2.7.2@pda/gems/money-6.19.0/money.gemspec'
load '~/.rvm/gems/ruby-2.7.2@pda/gems/monetize-1.13.0/monetize.gemspec'
load '~/.rvm/gems/ruby-2.7.2@pda/gems/money-rails-1.15.0/money-rails.gemspec'
To process the us_list field, I did this:
us_list = " $42.00 " # test code, not in production
temp1 = row[:us_list].strip
temp2 = temp1[1..]
slip_hash{:us_list] = Monetize.parse(temp2).format
If the field value is, e.g. ” $42.00 “, it displays as 0.0. If I delete the .format
, it displays as 42.0, which is at least an improvement.
slip_hash[:us_list] = Monetize.parse(temp2)
If I go into Rails Console, and I type
us_list = " $42.00 "
temp1 = us_list.strip
temp2 = temp1 [1..]
Monetize.parse(temp2).format
It displays “$42.00”, which is correct.
I also tried number_to_currency. I put the include statements at the top of the same file above. I’m doing a shorter version of the assignments to save space
I found this: /questions/5176718/how-to-use-the-number-to-currency-helper-method-in-the-model-rather-than-view,
include ActiveSupport::NumberHelper
slip_hash[:us_list] = number_to_currency("42")
The output is 0.0 in the app. If I do the following in the rails console
include ActiveSupport::NumberHelper
number_to_currency("42")
It displays “$42.00”, which is correct.
I found this: https://api.rubyonrails.org/v4.2.0/classes/ActionView/Helpers/NumberHelper.html,
include ActionView::Helpers::NumberHelper
slip_hash[:us_list] = number_to_currency("42")
The output is 0.0 in the app. If I do the following in the rails console
include ActionView::Helpers::NumberHelper
number_to_currency("42")
It displays “$42.00”, which is correct.
The monetize code also gives me an error message in Rails Console:
[WARNING] The default currency will change from `USD` to `nil` in the next major release. Make sure to set it explicitly using `Money.default_currency=` to avoid potential issues
[WARNING] The default rounding mode will change from `ROUND_HALF_EVEN` to `ROUND_HALF_UP` in the next major release. Set it explicitly using `Money.rounding_mode=` to avoid potential problems.
[DEPRECATION] You are using the default localization behaviour that will change in the next major release. Find out more - https://github.com/RubyMoney/money#deprecation.
I therefore added the following code at the top of the file:
#https://github.com/RubyMoney/money#deprecation
Money.locale_backend = :i18n
I18n.locale = :en
#https://rick-moore.medium.com/formatting-number-strings-in-ruby-4da35d5282e3
Money.rounding_mode = BigDecimal::ROUND_HALF_EVEN
#Money.locale_backend = :currency
Money.default_currency = "USD"
And I ran into some problems making the require/load work:
For some reason I couldn’t get
require ‘money’
require ‘monetize’
to work. I tried a lot of combinations and found the load statements above by trial and error and googling. I’m including this info in case it’s impacting the rest of my code.
More info:
Language Version:
- Rails 7.0.3
- ruby 2.7.2p137 (2020-10-01 revision 5445e04352) [x86_64-linux]
Operating System: Ubuntu 22.04.3 LTS (on Windows 10 Enterprise, 64-bit)
Database: sqlite3, 1.4.2
I am using the local version – the development version is not deployed. I’m using Chrome Version 126.0.6478.183 (Official Build) (64-bit)
I am writing my code in Visual Studio Code v 1.91.1
Jennifer James is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.