Query decimal precision?
nathanl opened this issue · comments
I want to validate that user-provided data is not excessively precise. In one case, 1 place after the decimal is the max allowed, so #Decimal<70>
is allowed and #Decimal<70.1>
is allowed but #Decimal<70.12>
is not.
Is there a straightforward way to check this? Ideally I'd do Decimal.precision(d) <= 1
. Here's a clunky way:
d = Decimal.new("12.1000") # => #Decimal<12.1000>
d = Decimal.normalize(d) # => #Decimal<12.1>
Decimal.round(d, 0) == d or Decimal.round(d, 1) == d # => true
Is #Decimal<70.10>
allowed? If it is not, that is, we just look at the number of digits after the decimal point, I believe the conditional can be simply d.exp > -2
:
iex> Decimal.new("70").exp > -2
true
iex> Decimal.new("70.1").exp > -2
true
iex> Decimal.new("70.10").exp > -2
false
Closed by #191. Thanks again!