dohutil 0.2.9 → 0.2.10

Sign up to get free protection for your applications and to get access to all the features.
@@ -2,9 +2,7 @@ require 'bigdecimal'
2
2
  require 'bigdecimal/util'
3
3
 
4
4
  class Integer
5
- # to_d is already defined in 1.9.3
6
5
  unless method_defined?(:to_d)
7
- puts "defining Integer.to_d"
8
6
  def to_d
9
7
  to_s.to_d
10
8
  end
@@ -12,9 +10,7 @@ class Integer
12
10
  end
13
11
 
14
12
  class BigDecimal
15
- # to_d is already defined in 1.9.3
16
13
  unless method_defined?(:to_d)
17
- puts "defining BigDecimal.to_d"
18
14
  def to_d
19
15
  self
20
16
  end
@@ -26,8 +22,15 @@ class BigDecimal
26
22
  end
27
23
 
28
24
  def to_dig(digits_after_decimal = 2)
29
- raise ArgumentError.new("digits_after_decimal must be > 0") unless digits_after_decimal > 0
30
- return '0.' + ('0' * digits_after_decimal) if nan? || infinite?
25
+ raise ArgumentError.new("digits_after_decimal must be >= 0") unless digits_after_decimal >= 0
26
+ if nan? || infinite?
27
+ return '0' if digits_after_decimal == 0
28
+ return '0.' + ('0' * digits_after_decimal)
29
+ end
30
+
31
+ if digits_after_decimal == 0
32
+ return to_i.to_s
33
+ end
31
34
 
32
35
  retval = truncate(digits_after_decimal).to_s('F')
33
36
  digits_needed = retval.index('.') + digits_after_decimal + 1 - retval.size
@@ -13,10 +13,12 @@ class Test_core_ext_bigdecimal < DohTest::TestGroup
13
13
 
14
14
  def test_to_dig_errors
15
15
  assert_raises(ArgumentError) { BigDecimal('1').to_dig(-1) }
16
- assert_raises(ArgumentError) { BigDecimal('1').to_dig(0) }
17
16
  assert_equal('0.00', BigDecimal('NaN').to_dig)
18
17
  assert_equal('0.00', BigDecimal('Infinity').to_dig)
19
18
  assert_equal('0.00', BigDecimal('blah').to_dig)
19
+ assert_equal('0', BigDecimal('NaN').to_dig(0))
20
+ assert_equal('1', BigDecimal('1').to_dig(0))
21
+ assert_equal('0', BigDecimal('blah').to_dig(0))
20
22
  end
21
23
 
22
24
  def test_to_dig_doesnt_modify
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: dohutil
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.9
4
+ version: 0.2.10
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -10,7 +10,7 @@ authors:
10
10
  autorequire:
11
11
  bindir: bin
12
12
  cert_chain: []
13
- date: 2013-04-04 00:00:00.000000000 Z
13
+ date: 2013-04-26 00:00:00.000000000 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: dohroot