2

I'm facing a problem with Rails application to store decimals in my mysql database. I have a form with two fields: "amount" and "currency". When I enter a decimal value in the "amount" field (for example 1,22) Rails just stores the 1 in the database.

My log file looks like this:

Started POST "/cashamounts" for 127.0.0.1 at 2012-02-04 13:23:54 +0100
  Processing by CashamountsController#create as HTML
  Parameters: {"utf8"=>"✓","authenticity_token"=>"QpWGfEtDR1tc7wTFmZZst9gYjKAyXtRypilsxDE9Tzs=", "cashamount"=>{"currency_id"=>"eur", "amount"=>"1,22"}, "commit"=>"Create Cashamount"}
  [1m[36mCurrency Load (2.8ms)[0m  [1mSELECT `currencies`.* FROM `currencies` ORDER BY name[0m
  [1m[35m (0.2ms)[0m  BEGIN
  [1m[36mSQL (0.3ms)[0m  [1mINSERT INTO `cashamounts` (`amount`, `created_at`, `currency_id`, `updated_at`) VALUES (1, '2012-02-04 12:23:54', 'eur', '2012-02-04 12:23:54')[0m
  [1m[35m (0.6ms)[0m  COMMIT
Redirected to http://localhost:3000/cashamounts/8
Completed 302 Found in 94ms

So the amount is stored in the params correctly, but is not inserted correctly into the database. I checked my db/schema.rb file. The line for the column "amount" is:

t.decimal  "amount",               :precision => 10, :scale => 2

Where else can I look to find the problem?

PS. I started off with using the money gem, but that showed the same problem: all digits after the "," are not stored.

3
  • What happens if you try a decimal point as in 1.22? Commented Feb 4, 2012 at 12:56
  • It also does not work with 1.22 as notation: Parameters: {"utf8"=>"✓", "authenticity_token"=>"QpWGfEtDR1tc7wTFmZZst9gYjKAyXtRypilsxDE9Tzs=", "cashamount"=>{"currency_id"=>"", "amount2"=>"1.22"}, "commit"=>"Create Cashamount"} [1m[35mCurrency Load (3.3ms)[0m SELECT currencies.* FROM currencies ORDER BY name [1m[36m (0.2ms)[0m [1mBEGIN[0m [1m[35mSQL (0.5ms)[0m INSERT INTO cashamounts (amount, created_at, currency_id, updated_at) VALUES (1, '2012-02-04 13:05:27', '', '2012-02-04 13:05:27') Commented Feb 4, 2012 at 13:08
  • Have you forgotten to run a migration or something? Check the actual DB schema (via MySQL itself) to ensure it is not an INT field. Commented Feb 4, 2012 at 22:55

3 Answers 3

6

This question is a bit old, but for future visitors I believe the answer can be found in the "Gotcha" section of:

http://torontoprogrammer.ca/2010/05/decimal-numbers-in-rails-and-mysql/.

Basically, when you set up the decimal field you have to specify the scale argument to say how many places should be to the right of the decimal point. Otherwise, mysql assumes that you want all of your digits to the left of the decimal point.

This problem bit me too. Everything worked fine in my dev environment (using sqlite), but in production (using mysql), the fractional part of my "decimal" numbers was getting truncated.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for this response. Kind of seems like rails should default to a scale of 2 or something...
2

1,22 is the European notation for decimal values, while 1.22 is the British, American and Australian convention. You may need to set your locale.

http://guides.rubyonrails.org/i18n.html#optional-custom-i18n-configuration-setup

In an initializer:

I18n.default_locale = :fr

Or at the end of config/application.rb

config.i18n.default_locale = :fr

1 Comment

unfortunately this did not solve the problem. my log for inserting looks still the same as the one i posted above..
0

The problem is that Rails expects a decimal point and not a comma. If this is user input try substituting the , for . before saving to the database.

1 Comment

A comma is a decimal point in most of Europe and parts of Asia.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.