checkin from browser
authorClaus Gittinger <cg@exept.de>
Tue, 26 Nov 2002 11:45:55 +0100
changeset 6895 2434bee328c1
parent 6894 64f27ed2753f
child 6896 ebfdcf4e35e5
checkin from browser
FixedPoint.st
--- a/FixedPoint.st	Tue Nov 26 11:41:25 2002 +0100
+++ b/FixedPoint.st	Tue Nov 26 11:45:55 2002 +0100
@@ -15,10 +15,10 @@
 "{ Package: 'stx:libbasic' }"
 
 Fraction subclass:#FixedPoint
-        instanceVariableNames:'scale'
-        classVariableNames:'PI_1000'
-        poolDictionaries:''
-        category:'Magnitude-Numbers'
+	instanceVariableNames:'scale'
+	classVariableNames:'PI_1000'
+	poolDictionaries:''
+	category:'Magnitude-Numbers'
 !
 
 FixedPoint comment:'
@@ -189,6 +189,61 @@
     "
 
     "Modified: / 25.10.1997 / 15:30:29 / cg"
+!
+
+readFrom:aStringOrStream onError:exceptionBlock
+    "return an instance of me as described on the string or stream, aStringOrStream.
+     If an error occurs during conversion, return the result
+     from evaluating exceptionBlock"
+
+    | aStream sign integerPart fractionStream char fractionPart scale |
+
+    aStream := aStringOrStream readStream.
+
+    aStream peek == $- ifTrue:[
+        sign := -1.
+        aStream next.
+    ] ifFalse:[
+        sign := 1
+    ].
+
+    (aStream atEnd or:[aStream peek isLetter]) ifTrue: [^ exceptionBlock value].
+
+    integerPart := (aStream upTo:$.) asNumber.
+    (aStream atEnd or: [aStream peek isLetter]) ifTrue: [
+        fractionPart := 0.
+        scale := 1.
+    ] ifFalse:[
+        fractionStream := ReadWriteStream on:(String new: 10).
+        [
+            char := aStream next.
+            char ~~ nil and:[char isDigit]
+        ] whileTrue:[
+            fractionStream nextPut:char
+        ].
+
+        scale := fractionStream positionStartingAt0.
+        fractionStream reset.
+        fractionPart := Number readFrom:fractionStream.
+    ].
+
+    ^ self basicNew 
+        setNumerator:(integerPart * (10 raisedTo:scale) + fractionPart) * sign
+        scale:scale
+
+    "
+     FixedPoint readFrom:'1.00' 
+     FixedPoint readFrom:'123.456'
+     FixedPoint readFrom:'-123.456' 
+     FixedPoint readFrom:'123' 
+     FixedPoint readFrom:'-123' 
+     FixedPoint readFrom:'-123.abcd' onError:[47.5] 
+     FixedPoint readFrom:'-1a.bcd' onError:[47.5] 
+     FixedPoint readFrom:'foot' onError:['bad fixedpoint'] 
+    "
+
+    "Created: / 25.10.1997 / 15:28:59 / cg"
+    "Modified: / 25.10.1997 / 15:31:47 / cg"
 ! !
 
 !FixedPoint class methodsFor:'constants'!
@@ -905,5 +960,5 @@
 !FixedPoint class methodsFor:'documentation'!
 
 version
-    ^ '$Header: /cvs/stx/stx/libbasic/FixedPoint.st,v 1.22 2002-11-26 10:41:25 cg Exp $'
+    ^ '$Header: /cvs/stx/stx/libbasic/FixedPoint.st,v 1.23 2002-11-26 10:45:55 cg Exp $'
 ! !