Converting a string with a decimal number that is less than zero and greater than -1, such as '-0.1', using an unspecific conversion asNumber or Number fromString: or fromStream:, results in a positive value, the absolute value of the correct result.
This applies to CharacterCollection >> asNumber, Number class >> fromString:, and Number class >> fromStream:.
This does not affect asFloat, nor BinaryFloat>> fromStream:/String:.
The patch below fixes issues with asNumber, but not Number class methods.
category: 'Converting' method: CharacterCollection asNumber "patch for bug 50751" "Returns the receiver converted to a kind of number. If the receiver contains all digits (with optional radix notation), returns a kind of Integer. If the receiver has a slash, returns a Fraction. Otherwise conversion to a Float is attempted. An error may result if the receiver does not contain the proper format for a kind of Number." | str strm sign res | str := self trimWhiteSpace. str size == 0 ifTrue: [ ^0 ]. (str at: 1) == $- ifTrue:[ strm := ReadStreamPortable on: (str copyFrom: 2 to: str size). sign := -1 . ] ifFalse:[ strm := ReadStreamPortable on: str . ]. (self indexOf: $/ startingAt: 1) ~~ 0 ifTrue:[ res := Fraction fromStream: strm . ] ifFalse:[ res := Number fromStream: strm . ]. sign ifNotNil:[ res := res * sign ]. ^ res %
Last updated: 11/2/23