Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 577979
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T14:12:43+00:00 2026-05-13T14:12:43+00:00

Looking on SO, I see that the preferred way to currency using RoR is

  • 0

Looking on SO, I see that the preferred way to currency using RoR is using decimal(8,2) and to output them using number_to_currency();

I can get my numbers out of the DB, but I’m having issues on getting them in.

Inside my update action I have the following line:

if @non_labor_expense.update_attributes(params[:non_labor_expense]) 
puts YAML::dump(params) 

The dump of params shows the correct value. xx,yyy.zz , but what gets stored in the DB is only xx.00

What do I need to do in order to take into account that there may be commas and a user may not enter .zz (the cents). Some regex and for comma? how would you handle the decimal if it were .2 versus .20 .

There has to be a builtin or at least a better way.

My Migration (I don’t know if this helps):

class ChangeExpenseToDec < ActiveRecord::Migration
    def self.up
       change_column :non_labor_expenses, :amount, :decimal, :precision => 8, :scale => 2
    end

    def self.down
          change_column :non_labor_expenses, :amount, :integer
    end
end
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T14:12:44+00:00Added an answer on May 13, 2026 at 2:12 pm

    It might depend on what DBMS you’re using, but as far as I know, decimal fields won’t accept commas (at least not as separators; there might be a way to have the database accept a comma as a decimal point rather than a period). What you will have to do is remove the commas from your numbers (in a before_save or before_validation filter, perhaps), and then when you display the number, add the commas back in.

    before_validation :strip_commas_from_non_labor_expense
    
    def strip_commas_from_non_labor_expense
      self.non_labor_expense = self.non_labor_expense.to_s.gsub(/,/, '').to_f
    end
    

    Then use number_to_currency when you want to display the expense amount formatted with comma separated groups and two decimal places, as you mentioned:

    <%
      non_labor_expense = ... # get value from your model
      puts number_to_currency(non_labor_expense, :precision => 2, :separator => ',')
    %>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Looking at examples about socket programming, we can see that some people use AF_INET
I'm looking to see if I can design a HtmlHelper extension method that will
Looking through the documentation for CFDataRef I can't see anything that will compress a
Looking at the image below, you can see that there are lots of javascript
I am looking at the Prism Stocktrader RI and I can see that the
Have been looking at the MVC storefront and see that IQueryable is returned from
Looking through my server logs, I see that a lot of pages on my
Im looking at jqueries tabs, datepicker etc and see that font-sizes of the classes
I am looking for a fast queue implementation in Java. I see that LinkedList
I am looking to see if there is an open source/free project that provides

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.