简体   繁体   English

Ruby on Rails不会在mysql数据库中存储小数:之后截断数字,

[英]Ruby on Rails doesn't store decimals in mysql database: truncates numbers after ,

I'm facing a problem with Rails application to store decimals in my mysql database. 我在使用Rails应用程序时遇到问题,要在我的mysql数据库中存储小数。 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. 当我在“amount”字段中输入十进制值时(例如1,22),Rails只将1存储在数据库中。

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. 我检查了我的db / schema.rb文件。 The line for the column "amount" is: 列“金额”的行是:

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

Where else can I look to find the problem? 我还能在哪里找到问题?

PS. PS。 I started off with using the money gem, but that showed the same problem: all digits after the "," are not stored. 我开始使用money gem,但是显示了同样的问题:“,”之后的所有数字都没有存储。

This question is a bit old, but for future visitors I believe the answer can be found in the "Gotcha" section of: 这个问题有点旧,但对于未来的访问者,我相信答案可以在以下的“Gotcha”部分找到:

http://torontoprogrammer.ca/2010/05/decimal-numbers-in-rails-and-mysql/ . 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. 基本上,当你设置十进制字段时,你必须指定scale参数来说明小数点右边应该有多少个位置。 Otherwise, mysql assumes that you want all of your digits to the left of the decimal point. 否则,mysql假定您希望所有数字都在小数点左侧。

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. 一切都在我的开发环境中工作正常(使用sqlite),但在生产中(使用mysql),我的“十进制”数字的小数部分被截断。

1,22 is the European notation for decimal values, while 1.22 is the British, American and Australian convention. 1,22是十进制值的欧洲表示法,而1.22是英国,美国和澳大利亚的惯例。 You may need to set your locale. 您可能需要设置您的区域设置。

http://guides.rubyonrails.org/i18n.html#optional-custom-i18n-configuration-setup 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 / application.rb的末尾

config.i18n.default_locale = :fr

The problem is that Rails expects a decimal point and not a comma. 问题是Rails需要小数点而不是逗号。 If this is user input try substituting the , for . 如果这是用户输入,请尝试替换, for . before saving to the database. 在保存到数据库之前。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM