[RAILS] [Ruby] Big Decimal and DECIMAL

I compared Decimal type columns with numbers but it didn't work as expected

def show
  @item = Item.find(params[:id])
  if @item.price < 1 #price is DECIMAL type
    #Write some processing
  end
end

When I check the type, it is String. Apparently Decimal is a fixed-point number type and handles more accurate values than floating-point types, so it seems that it will be returned as a character string (naturally ...?)

 logger.debug(@item.price.class)
 -> String

approach

It works as expected when using the BigDecimal library

require 'bigdecimal'

if BigDecimal(@item.price) < 1
    #Write some processing
end

What are floating point and fixed point in the first place?

Floating-point types such as Float can handle a large number of digits, but errors occur because the numbers after the decimal point cannot be represented by binary numbers. → Rounding error

The fixed-point type solves this problem, but it can handle fewer digits than the floating-point type.

reference

・ Https://docs.ruby-lang.org/ja/latest/library/bigdecimal.html ・ Https://qiita.com/jnchito/items/d0ef71b25732ad5a881c ・ Https://qiita.com/angel_p_57/items/24078ba4aa5881805ab2

Recommended Posts

[Ruby] Big Decimal and DECIMAL
Java Big Decimal
Ruby and Gem
[Ruby] Classes and instances
Symbols and Destructive Ruby
Big Decimal in Kotlin
Ruby classes and instances
Ruby inheritance and delegation
Ruby variables and methods
GraphQL Ruby and actual development
Ruby syntax errors and countermeasures
How to use Big Decimal
About Ruby hashes and symbols
Ruby C extension and volatile
Summarize Ruby and Dependency Injection
[Java] Use Big Decimal properly ~ 2018 ~
About Ruby and object model
[Ruby] Singular methods and singular classes
About Ruby classes and instances
Ruby variables and functions (methods)
Ruby methods and classes (basic)
Creating Ruby classes and instances
[Ruby] Singular methods and singular classes
[Ruby] Difference between get and post
[Ruby] present/blank method and postfix if.
Ruby standard input and various methods
About Ruby single quotes and double quotes
[Ruby] then keyword and case in
[Ruby basics] split method and to_s method
About Ruby product operator (&) and sum operator (|)
Write keys and values in Ruby
[Ruby] If and else problems-with operators-
[Ruby] Boolean values ​​and logical operators
Project ruby and rails version upgrade
About object-oriented inheritance and about yield Ruby
[Java] Tips and error issues when converting from double to Big Decimal
With ruby ● × Game and Othello (basic review)
Upgrade and switch Ruby versions on Windows 10
Note: Difference between Ruby "p" and "puts"
Make bubble sort and selection sort in Ruby
Difference between Ruby instance variable and local variable
[Ruby] Simplify each using map and inject
Explanation of Ruby Time and Date objects
Summary of hashes and symbols in Ruby
Memorandum (Ruby: Basic Grammar: Classes and Instances)
ruby Exercise Memo II (variable and method)
[Ruby] Classification and usage of loops in Ruby
Difference between "|| =" and "instance_variable_defined?" In Ruby memoization
[Easy] How to upgrade Ruby and bundler
Comparison of JavaScript objects and Ruby classes
Write code using Ruby classes and instances
[Ruby] Difference between print, puts and p
Don't get hooked on Java's Big Decimal
Differences between Ruby strings and symbols [Beginner]
Big Decimal memorandum of always int beginner
Convert JSON to TSV and TSV to JSON with Ruby
[Ruby] Calculation results between decimal points are different, different, and not what you intended.