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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T16:47:02+00:00 2026-06-01T16:47:02+00:00

I am making a simple retail commerce solution, where there are prices in a

  • 0

I am making a simple retail commerce solution, where there are prices in a few different models. These prices contribute to a total price. Imagine paying $0.30 more for selecting a topping for your yogurt.

When I set the price field to

t.decimal   :price, precision:8, scale:2

The database stores 6.50 as 6.5. I know in the standard rails way, you call number_to_currency(price) to get the formatted value in the Views. I need to programmatically call the price field as well formatted string, i.e. $6.50 a few places that are not directly part of the View. Also, my needs are simple (no currency conversion etc), I prefer to have the price formatted universally in the model without repeated calling number_to_currency in views.

Is there a good way I can modify my getter for price such that it always returns two decimal place with a dollar sign, i.e. $6.50 when it’s called?

Thanks in advance.

UPDATE

Thanks everyone.

I’ve elected to use Alex’s approach because it seems very ‘hackish’ to do the includes just for formatting the number. Using his approach, I did:

  def price_change=(val)
      write_attribute :price_change, val.to_s.gsub(/[\$]/,'').to_d
  end

  def price_change
    "$%.2f" % self[:price_change]
  end

Cheers.

UPDATE 2

Caveat Emptor. Once you do this, you lose the ability to do operations to the number because it’s now a string.

Please beware if anyone is facing the same problem as me.

  • 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-01T16:47:03+00:00Added an answer on June 1, 2026 at 4:47 pm

    Just add a method in your model which is named like your attribute in the database like:

    def price
       "$%.2f" % self[:price]
    end
    

    which gives you full control over the formatting or use the Rails provided helper method

    def price
       ActionController::Base.helpers.number_to_currency(self[:price])
    end
    

    this should do the trick.
    hope it helps!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm making simple C# Winform application. There is a Form having a textbox. I
Im making a simple app for some friends to use on there android phone
I am making simple application that will take hotel name and price of item,
I am making simple top down game. There are 8 possible directions of car
I am making simple connection between two table - first one called users has
Im making a simple alternative to the default linux system monitor. Im looking to
in making a simple cgi server for a course. To do that in some
I’m making a simple LOB app which loads data from an XML file and
I'm making a simple program in Objective-C. It has one class with a lot
I am making a simple multiplayer economic game in pygame. It consists of turns

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.