Why is the data in a Rails view displaying as 0.0 or 42.0 instead of $42.00?

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

New contributor

Jennifer James is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật