#FEATURE by cg
authorClaus Gittinger <cg@exept.de>
Fri, 11 May 2018 02:03:58 +0200
changeset 22895 711322e64257
parent 22894 81be3c10fb6c
child 22896 0a1a1f40be28
#FEATURE by cg class: Fraction class changed: #readDecimalFractionFrom:onError:
Fraction.st
--- a/Fraction.st	Fri May 11 01:11:31 2018 +0200
+++ b/Fraction.st	Fri May 11 02:03:58 2018 +0200
@@ -1,3 +1,5 @@
+"{ Encoding: utf8 }"
+
 "
  COPYRIGHT (c) 1989 by Claus Gittinger
 	      All Rights Reserved
@@ -163,26 +165,31 @@
 readDecimalFractionFrom:aStringOrStream onError:exceptionBlock
     "Read an arbitrary number (>0) of digits representing a decimal fraction."
 
-    |anyDigit factor fraction s ch|
+    |numDigits factor fraction s ch denom|
 
     s := aStringOrStream isStream ifTrue:[aStringOrStream] ifFalse:[aStringOrStream readStream].
 
     factor := (1 / 10).
     fraction := 0.
-    anyDigit := false.
+    numDigits := 0.
 
     [
-	ch := s peekOrNil.
-	ch notNil and:[ch isDigit].
+        ch := s peekOrNil.
+        ch notNil and:[ch isDigit].
     ] whileTrue: [
-	s next.
-	anyDigit := true.
-	fraction := (ch digitValue) * factor + fraction.
-	factor := (factor / 10).
+        s next.
+        fraction := fraction*10 + (ch digitValue).
+        numDigits := numDigits + 1.
     ].
 
-    anyDigit ifFalse: [^ exceptionBlock valueWithOptionalArgument: 'Missing digits in fraction'].
-    ^ fraction
+    numDigits > 0 ifFalse:[^ exceptionBlock valueWithOptionalArgument: 'Missing digits in fraction'].
+    numDigits <= 10 ifTrue:[
+        denom := #(10 100 1000 10000 100000
+                   1000000 10000000 100000000 1000000000 10000000000) at:numDigits.
+    ] ifFalse:[
+        denom := 10 raisedTo:numDigits.
+    ].
+    ^ fraction / denom
 
     "
      Fraction readDecimalFractionFrom:'1'   onError:[nil]     -> 0.1
@@ -1165,6 +1172,7 @@
     "Modified: 28.7.1997 / 19:08:40 / cg"
 ! !
 
+
 !Fraction methodsFor:'printing & storing'!
 
 printOn:aStream