开发者

How to enforce the number of significant digits of a BigDecimal

开发者 https://www.devze.com 2023-02-10 02:37 出处:网络
I defined a decimal field with a scale / significant digits of 4 in mysql (ex. 10.0001 ). ActiveRecord returns it as a BigDecimal.

I defined a decimal field with a scale / significant digits of 4 in mysql (ex. 10.0001 ). ActiveRecord returns it as a BigDecimal.

I can set the field in ActiveRecord with a scale of 5 (ex. 10.00001 ), and save it, which effectively truncates the value (it's stored as 10.0000).

Is there a way to prevent this? I already looked at the BigDecimal class 开发者_如何学Pythonif there is a way to force scale. Couldn't find one. I can calculate the scale of a BigDecimal and return a validation error, but I wonder if there is a nicer way to enforce it.


You could add a before_save handler for your class and include logic to round at your preference, for example:

class MyRecord < ActiveRecord::Base
  SCALE = 4
  before_save :round_decimal_field
  def round_decimal_field
    self.decimal_field.round(SCALE, BigDecimal::ROUND_UP)
  end
end
r = MyRecord.new(:decimal_field => 10.00009)
r.save!
r.decimal_field # => 10.0001

The scale factor might even be assignable automatically by reading the schema somehow.

See the ROUND_* constant names in the Ruby BigDecimal class documentation for other rounding modes.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号