FixedPoint.st
changeset 24994 24d2a0b0a027
parent 21975 1701bf8eb8e3
child 25007 050a1002f4b6
equal deleted inserted replaced
24993:f64786c96b57 24994:24d2a0b0a027
   221 
   221 
   222 readFrom:aStringOrStream decimalPointCharacters:decimalPointCharacters onError:exceptionBlock
   222 readFrom:aStringOrStream decimalPointCharacters:decimalPointCharacters onError:exceptionBlock
   223     "return an instance of me as described on the string or stream, aStringOrStream.
   223     "return an instance of me as described on the string or stream, aStringOrStream.
   224      If an error occurs during conversion, return the result from evaluating exceptionBlock"
   224      If an error occurs during conversion, return the result from evaluating exceptionBlock"
   225 
   225 
   226     | aStream sign integerPart fractionStream char fractionPart scale nextChar |
   226     | aStream sign integerPart fractionStream char fractionPart scale nextChar val|
   227 
   227 
   228     aStream := aStringOrStream readStream.
   228     aStream := aStringOrStream readStream.
   229 
   229 
   230     aStream peekOrNil == $- ifTrue:[
   230     aStream peekOrNil == $- ifTrue:[
   231         sign := -1.
   231         sign := -1.
   244         (nextChar isDigit) ifFalse: [^ exceptionBlock value].
   244         (nextChar isDigit) ifFalse: [^ exceptionBlock value].
   245         "/ FIX: only read the integer chars - not to the end.
   245         "/ FIX: only read the integer chars - not to the end.
   246         integerPart := Integer readFrom:aStream "(aStream upToAny:decimalPointCharacters)" allowRadix:false onError:[^ exceptionBlock value].
   246         integerPart := Integer readFrom:aStream "(aStream upToAny:decimalPointCharacters)" allowRadix:false onError:[^ exceptionBlock value].
   247         nextChar := aStream peekOrNil.
   247         nextChar := aStream peekOrNil.
   248         (decimalPointCharacters includes:nextChar) ifFalse:[
   248         (decimalPointCharacters includes:nextChar) ifFalse:[
       
   249             sign < 0 ifTrue:[ integerPart := integerPart negated ].
   249             "/ only integer part
   250             "/ only integer part
   250             ^ self basicNew 
   251             ^ self basicNew 
   251                 setNumerator:integerPart * sign
   252                 setNumerator:integerPart
   252                 denominator:1
   253                 denominator:1
   253                 scale:0
   254                 scale:0
   254         ].    
   255         ].    
   255         aStream next.
   256         aStream next.
   256     ].
   257     ].
   257     nextChar := aStream peekOrNil.
   258     nextChar := aStream peekOrNil.
   258     (nextChar isNil or:[nextChar isDigit not]) ifTrue: [
   259     (nextChar isNil or:[nextChar isDigit not]) ifTrue: [
       
   260         sign < 0 ifTrue:[ integerPart := integerPart negated ].
   259         ^ self basicNew 
   261         ^ self basicNew 
   260             setNumerator:integerPart * sign
   262             setNumerator:integerPart
   261             denominator:1
   263             denominator:1
   262             scale:0
   264             scale:0
   263     ].
   265     ].
   264     
   266     
   265     fractionStream := ReadWriteStream on:(String new:10).
   267     fractionStream := ReadWriteStream on:(String new:10).
   271     ].
   273     ].
   272 
   274 
   273     scale := fractionStream position.
   275     scale := fractionStream position.
   274     fractionStream reset.
   276     fractionStream reset.
   275     fractionPart := Integer readFrom:fractionStream onError:[^ exceptionBlock value]. 
   277     fractionPart := Integer readFrom:fractionStream onError:[^ exceptionBlock value]. 
   276 
   278     val := integerPart * (10 raisedTo:scale) + fractionPart.
       
   279     sign < 0 ifTrue:[ val := val negated ].
   277     ^ self basicNew 
   280     ^ self basicNew 
   278         setNumerator:(integerPart * (10 raisedTo:scale) + fractionPart) * sign 
   281         setNumerator:val 
   279         scale:scale
   282         scale:scale
   280 
   283 
   281     "
   284     "
   282      FixedPoint readFrom:'1.00'    
   285      FixedPoint readFrom:'1.00'    
   283      FixedPoint readFrom:'123.456'  
   286      FixedPoint readFrom:'123.456'