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 8120551
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T05:11:19+00:00 2026-06-06T05:11:19+00:00

Here’s what I’m using in my model: before_validation :strip_dollar_sign validates :amount_due, :format => {

  • 0

Here’s what I’m using in my model:

before_validation :strip_dollar_sign

validates :amount_due,
          :format => { :with => /^\d+??(?:\.\d{0,2})?$/ }, 
          :numericality => {:greater_than => 0}

private

def strip_dollar_sign
  self.amount_due = self.amount_due.to_s.tr!('$,','').to_f
end

If I run the line from the strip_dollar_sign function by hand in the Rails Console I get exactly what I want (i.e. $400 ends up as 400.0) but when I use the actual form in my app the value always ends up as 0.0. Anybody catch what I’m doing wrong?

  • 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-06-06T05:11:20+00:00Added an answer on June 6, 2026 at 5:11 am

    Three problems here:

    • As pointed out by Stefan in his answer, you may want to remove the , in your tr! call, though it won’t affect the replacement of a $.

    • You’re using tr!, and are using its return value in an incorrect way. tr! (along with most of Ruby’s ! method variants) mutates the string in-place and returns nil if no changes were made. Since nil.to_f is 0.0, that’s why you’re getting that (or maybe not, see below). You should instead use tr.

    • Rails automatically converts assignment arguments to the correct type for the database column associated with it, so even before validation your value is being converted to a float, and "$400".to_f is 0.0, and that’s what your callback sees. The solution is to override amount_due= instead of using a callback:

      def amount_due=(value)
        value = value.to_s.tr('$', '').to_f
        write_attribute(:amount_due, value)
      end
      
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Here is a complete example. I want to forbid using A::set from objects casted
Here my problem: @Assert\Regex( * pattern=/^[A-Za-z0-9][A-Za-z0-9\]*$/, * groups={creation, creation_logged} * ) I'm using the
Here is the code I'm using inside my AsyncTask DefaultHttpClient httpClient = new DefaultHttpClient();
Here is my class: public class A{ private void doIt(int[] X, int[] Y){ //change
Here is my simplified data structure: Object1.h template <class T> class Object1 { private:
here is my problem. I'm using MVC and in a lot of Index Views
Here is the code for the relevant function: def populateSubscribers(csvfile, db): conn = sqlite3.connect(db)
Here's my proposed (very simplified to illustrate the problem space) design for a C#
Here the total height of all <div> 's are 900 pixels, but the jQuery
Here's the deal: I'm in the process of planning a mid-sized business application that

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.