Project

General

Profile

Bug #12718

BigDecimal() should raise on invalid input, consistent with Integer() and Float()

Added by jrochkind (jonathan rochkind) over 2 years ago. Updated over 2 years ago.

Status:
Closed
Priority:
Normal
Target version:
-
ruby -v:
ruby 2.3.1p112 (2016-04-26 revision 54768) [x86_64-darwin15]
[ruby-core:77122]

Description

Integer("2") # => 2
Integer("invalid") # raises ArgumentError: invalid value for Integer()

Float("2.0")  # => 2.0
Float("invalid") # raises ArgumentError: invalid value for Float()

BUT

BigDecimal("2.2") # => #<BigDecimal:7fb9ab1a9d40,'0.22E1',18(18)>
BigDecimal("invalid") # => #<BigDecimal:7fb9ab1b28c8,'0.0',9(9)>

I think BigDecimal("invalid") should raise rather than returning a BigDecimal representing 0, to be consistent with Integer() and Float(), and to give you access to a string conversion that raises, which I don't think exists in stdlib at present.

If you want conversion of a string to a BigDecimal that returns 0 rather than raising on bad input, you have String#to_d in bigdecimal/util. That's consistent with String#to_f and #to_i as well. But there is currently no way to do a conversion that raises, BigDecimal() is inconsistent with Float() and Integer()


Related issues

Is duplicate of Ruby trunk - Bug #10286: BigDecimal - inconsistency with other numeric classesClosedActions

History

Updated by jrochkind (jonathan rochkind) over 2 years ago

Oops it ate my bigdecimal output

BigDecimal("2.2") # => BigDecimal '0.22E1',18(18)
BigDecimal("invalid") # => BigDecimal '0.0',9(9)

Updated by nobu (Nobuyoshi Nakada) over 2 years ago

  • Assignee set to mrkn (Kenta Murata)
  • Status changed from Open to Assigned
  • Description updated (diff)
#3

Updated by mrkn (Kenta Murata) over 2 years ago

  • Is duplicate of Bug #10286: BigDecimal - inconsistency with other numeric classes added

Updated by mrkn (Kenta Murata) over 2 years ago

  • Status changed from Assigned to Closed

Close this because it duplicates #10286

Also available in: Atom PDF