All threads / Handle money in Rails

Ask A Question

Notifications

You’re not receiving notifications from this thread.

Handle money in Rails

Wouter van den Beld asked in Rails

Hi,

What do you think is the best way to handle money in rails?
At the moment i use a decimal field with a precision if 16 and a scale of 2.

the problem is that we use the comma as a decimal sign and this sometimes becomes a problem.

should i use the money gem or use only cents or do you guys have a better way to handle this?

Oh that's a really interesting question. I've never really considered that because we only use it in the other notation.

I would imagine that the Money gem can do the conversion for you and that internally it really saves them as decimals but tweaks the string output so that it uses comma for the decimal. I think you would want to continue saving with the decimal format and then have a method that converts the value into a Money object and that would be what you interact with. You could also probably have a setter method that lets you set the value via the comma version of the string which converts to the decimal one in the database.

Join the discussion

Want to stay up-to-date with Ruby on Rails?

Join 33,399+ developers who get early access to new tutorials, screencasts, articles, and more.

    We care about the protection of your data. Read our Privacy Policy.

    logo Created with Sketch.

    Ruby on Rails tutorials, guides, and screencasts for web developers learning Ruby, Rails, Javascript, Turbolinks, Stimulus.js, Vue.js, and more. Icons by Icons8

    © 2020 GoRails, LLC. All rights reserved.