Ruby on Rails Models. Why does a decimal{2,1} with scope 1 allow more than digit after the decimal point? -


Despite defining the exact and the scope, there is a problem with the table accepting too many digits after the decimal Has been there.

  Construction of railway model Hotel Name: string 'Rating: Decimal {2,1}' Classroom Hotel & lt; ActiveRecord :: Migration Def Makeover make_table: hotels do | T | t.string: Name t.decimal: rating, exact: 2, scale: 1t.timestamps end and end   

However, I am able to do the following. Hotels Created! (Name: รข "The Holiday Inn, ????, Rating: 3.75)

Additionally, I have a room table (cell model), with < / p>

  t.decimal: rate, exact: 5, scale: 2 # this room's night rate hold   

I input 99.99 in this column , But it ends up storing in 99.98 99 99 99 ..

Why do I have these two decimal issues? If I have defined my scope, then why is it allowed to input more scope than defined?

I input 99.99 in this column, but it ends up storing in 99.98999999 ...

This suggests that you are using SQLite which is not really a decimal data type, if you have a SQLite To make the decimal (m, n) column, instead of making it a real ; the real is a floating point type, not a certain precision type. To resolve this issue, stop using SQLite; There are obstacles that you are not deploying any actual application at the top of the equity, so that you should develop with the same database you are going to deploy.

In addition, if you are using a specific precision type for the rating , you should not say:

  Hotel.create ! (Name: "Holiday Inn", Rating: 3.75) as   

that will be a floating point value in 3.75 rubles, and it may be messed up before viewing the database. You should say one of these instead:

  Hotel.create! (Name: "Holiday Inn", Rating: '3.75') Hotel.create! (Name: "Holiday Inn", "Holiday Inn" Rating: BigDecimal.new ('3.75'))   

so that you stay behind from floating point front.

Comments

Popular posts from this blog

Verilog Error: output or inout port "Q" must be connected to a structural net expression -

jasper reports - How to center align barcode using jasperreports and barcode4j -

c# - ASP.NET MVC - Attaching an entity of type 'MODELNAME' failed because another entity of the same type already has the same primary key value -