Skip to content

Commit

Permalink
convert price to bigdecimal, add to_s
Browse files Browse the repository at this point in the history
  • Loading branch information
pmkaboo committed Sep 22, 2017
1 parent 05c34b2 commit 0543e30
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 1 deletion.
13 changes: 13 additions & 0 deletions Gemfile.lock
@@ -0,0 +1,13 @@
GEM
remote: https://rubygems.org/
specs:
minitest (5.10.3)

PLATFORMS
ruby

DEPENDENCIES
minitest

BUNDLED WITH
1.11.2
11 changes: 10 additions & 1 deletion product.rb
@@ -1,8 +1,17 @@
require 'bigdecimal'
require 'bigdecimal/util'

class Product
attr_accessor :name, :price

def initialize name, price
@name = name
@price = price
@price = price.to_d

This comment has been minimized.

Copy link
@martincik

martincik Sep 22, 2017

Collaborator

What happens if I put as a price string or some other type that is not convertible to Decimal? (Imaging that someone makes mistake and puts there not number.)

This comment has been minimized.

Copy link
@pmkaboo

pmkaboo Sep 22, 2017

Author Owner

price string('123.12') will be converted, if you pass something thats not convertible, you get an error explaining what you did wrong

This comment has been minimized.

Copy link
@martincik

martincik Sep 22, 2017

Collaborator

Am I doing something wrong here?:

2.1.6 :001 > require 'bigdecimal'
 => true
2.1.6 :002 > require 'bigdecimal/util'
 => true
2.1.6 :003 > "xxx".to_d
 => #<BigDecimal:7fe26b14d020,'0.0',9(9)>
2.1.6 :004 > a = "xxx".to_d
 => #<BigDecimal:7fe26b1d47f0,'0.0',9(9)>
2.1.6 :005 > a
 => #<BigDecimal:7fe26b1d47f0,'0.0',9(9)>
2.1.6 :006 > a.to_s
 => "0.0"

This comment has been minimized.

Copy link
@pmkaboo

pmkaboo Sep 22, 2017

Author Owner

im not sure

2.4.1 :018 > 'asf'.to_d
ArgumentError: invalid value for BigDecimal(): "asf"
	from /home/omgowl/.rvm/rubies/ruby-2.4.1/lib/ruby/2.4.0/bigdecimal/util.rb:61:in `BigDecimal'
	from /home/omgowl/.rvm/rubies/ruby-2.4.1/lib/ruby/2.4.0/bigdecimal/util.rb:61:in `to_d'
	from (irb):18
	from /home/omgowl/.rvm/rubies/ruby-2.4.1/bin/irb:11:in `<main>'

with double quotes its the same

2.4.1 :020 > "xxx".to_d
ArgumentError: invalid value for BigDecimal(): "xxx"
	from /home/omgowl/.rvm/rubies/ruby-2.4.1/lib/ruby/2.4.0/bigdecimal/util.rb:61:in `BigDecimal'
	from /home/omgowl/.rvm/rubies/ruby-2.4.1/lib/ruby/2.4.0/bigdecimal/util.rb:61:in `to_d'
	from (irb):20
	from /home/omgowl/.rvm/rubies/ruby-2.4.1/bin/irb:11:in `<main>'

This comment has been minimized.

Copy link
@pmkaboo

pmkaboo Sep 22, 2017

Author Owner

alright, seems like in 2.4 bigdecimal was updated to raise exceptions on invalid input to be more consistent with other types, but this change also made string.to_d raise an error which is inconsistent with other numeric types. it was later fixed, but you need to manually update bigdecimal gem to get the fix. (ruby/bigdecimal#51)

in this case, we ofcourse need to raise an error, because silently changing it to 0.0 could lead to pretty big and hard to detect issues.

end

def to_s
# regex truncate to always display two decimals
name + ', ' + (price.to_s('F') + '0')[/.*\..{2}/]

This comment has been minimized.

Copy link
@martincik

martincik Sep 22, 2017

Collaborator

Wouldn't this be easier sprintf('%.2f', number)? Just out of curiosity, how did you come up with such solution?

This comment has been minimized.

Copy link
@pmkaboo

pmkaboo Sep 22, 2017

Author Owner

it would be easier, but if we do that, we might aswell just stay with floats, i found the solution through google. heres a number that sprintf wouldnt work with 400000000000000.2

This comment has been minimized.

Copy link
@martincik

martincik Sep 22, 2017

Collaborator

Very good! That explains it well.

end

end
8 changes: 8 additions & 0 deletions product_spec.rb
Expand Up @@ -17,4 +17,12 @@
@product.price.must_equal 123
end
end

describe '#to_s' do
it 'returns name and price with two decimals' do
@product.to_s.must_equal 'name, 123.00'
product2 = Product.new 'name2', 19.999
product2.to_s.must_equal 'name2, 19.99'
end
end
end

0 comments on commit 0543e30

Please sign in to comment.