Skip to content

Commit

Permalink
Add support for big decimals in formating numbers (thoughtbot#842)
Browse files Browse the repository at this point in the history
  • Loading branch information
BenMorganIO authored and iarie committed Jun 17, 2017
1 parent edc429e commit 2a2d024
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 3 deletions.
3 changes: 2 additions & 1 deletion lib/administrate/field/number.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ def suffix
end

def decimals
default = data.is_a?(Float) ? data.to_s.split(".").last.size : 0
_left, right = data.to_s.split(".")
default = right.nil? ? 0 : right.size
options.fetch(:decimals, default)
end

Expand Down
7 changes: 5 additions & 2 deletions spec/lib/fields/number_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,11 @@
end

it "defaults to the precision of the decimal" do
number = Administrate::Field::Number.new(:number, 12.123456, :page)
expect(number.to_s).to eq("12.123456")
float = Administrate::Field::Number.new(:number, 12.123456, :page)
big_decimal = Administrate::Field::Number.new(:number, 0.26186536e2, :page)

expect(float.to_s).to eq("12.123456")
expect(big_decimal.to_s).to eql("26.186536")
end
end

Expand Down

0 comments on commit 2a2d024

Please sign in to comment.