File tree 6 files changed +12
-0
lines changed
6 files changed +12
-0
lines changed Original file line number Diff line number Diff line change @@ -177,6 +177,7 @@ describe BigDecimal do
177
177
it " can be converted from other types" do
178
178
1 .to_big_d.should eq (BigDecimal .new(1 ))
179
179
" 1.5" .to_big_d.should eq (BigDecimal .new(15 , 1 ))
180
+ " +1.5" .to_big_d.should eq (BigDecimal .new(15 , 1 ))
180
181
BigInt .new(15 ).to_big_d.should eq (BigDecimal .new(15 , 0 ))
181
182
1.5 .to_big_d.should eq (BigDecimal .new(15 , 1 ))
182
183
1.5 .to_big_f.to_big_d.should eq (BigDecimal .new(15 , 1 ))
Original file line number Diff line number Diff line change @@ -11,6 +11,8 @@ describe "BigFloat" do
11
11
it " new(String)" do
12
12
bigfloat_of_integer_value.to_s.should eq(string_of_integer_value)
13
13
bigfloat_of_float_value.to_s.should eq(string_of_float_value)
14
+ BigFloat .new(" +#{ string_of_integer_value } " ).to_s.should eq(string_of_integer_value)
15
+ BigFloat .new(" -#{ string_of_integer_value } " ).to_s.should eq(" -#{ string_of_integer_value } " )
14
16
end
15
17
16
18
it " new(BigInt)" do
Original file line number Diff line number Diff line change @@ -22,6 +22,8 @@ describe "BigInt" do
22
22
23
23
it " creates from string" do
24
24
BigInt .new(" 12345678" ).to_s.should eq(" 12345678" )
25
+ BigInt .new(" +12345678" ).to_s.should eq(" 12345678" )
26
+ BigInt .new(" -12345678" ).to_s.should eq(" -12345678" )
25
27
end
26
28
27
29
it " raises if creates from string but invalid" do
Original file line number Diff line number Diff line change @@ -45,6 +45,9 @@ struct BigDecimal < Number
45
45
#
46
46
# Allows only valid number strings with an optional negative sign.
47
47
def initialize (str : String )
48
+ # Strip leading '+' char to smooth out cases with strings like "+123"
49
+ str = str.lchop('+' )
50
+
48
51
raise InvalidBigDecimalException .new(str, " Zero size" ) if str.bytesize == 0
49
52
50
53
# Check str's validity and find index of .
Original file line number Diff line number Diff line change @@ -13,6 +13,8 @@ struct BigFloat < Float
13
13
end
14
14
15
15
def initialize (str : String )
16
+ # Strip leading '+' char to smooth out cases with strings like "+123"
17
+ str = str.lchop('+' )
16
18
LibGMP .mpf_init_set_str(out @mpf , str, 10 )
17
19
end
18
20
Original file line number Diff line number Diff line change @@ -28,6 +28,8 @@ struct BigInt < Int
28
28
# BigInt.new("1234567890ABCDEF", base: 16) # => 1311768467294899695
29
29
# ```
30
30
def initialize (str : String , base = 10 )
31
+ # Strip leading '+' char to smooth out cases with strings like "+123"
32
+ str = str.lchop('+' )
31
33
err = LibGMP .init_set_str(out @mpz , str, base)
32
34
if err == -1
33
35
raise ArgumentError .new(" Invalid BigInt: #{ str } " )
You can’t perform that action at this time.
0 commit comments