--- a/Fraction.st Tue Feb 04 21:09:59 2014 +0100
+++ b/Fraction.st Wed Apr 01 10:20:10 2015 +0100
@@ -133,6 +133,40 @@
"
!
+readDecimalFractionFrom:aStringOrStream onError:exceptionBlock
+ "Read an arbitrary number (>0) of digits representing a decimal fraction."
+
+ |anyDigit factor fraction s ch|
+
+ s := aStringOrStream isStream ifTrue:[aStringOrStream] ifFalse:[aStringOrStream readStream].
+
+ factor := (1 / 10).
+ fraction := 0.
+ anyDigit := false.
+
+ [
+ ch := s peekOrNil.
+ ch notNil and:[ch isDigit].
+ ] whileTrue: [
+ s next.
+ anyDigit := true.
+ fraction := (ch digitValue) * factor + fraction.
+ factor := (factor / 10).
+ ].
+
+ anyDigit ifFalse: [^ exceptionBlock valueWithOptionalArgument: 'Missing digits in fraction'].
+ ^ fraction
+
+ "
+ Fraction readDecimalFractionFrom:'1' onError:[nil] -> 0.1
+ Fraction readDecimalFractionFrom:'123' onError:[nil] -> 0.123
+ Fraction readDecimalFractionFrom:'5' onError:[nil] -> 0.5
+ Fraction readDecimalFractionFrom:'005' onError:[nil] -> 0.005
+ Fraction readDecimalFractionFrom:'' onError:[nil] -> nil
+ Fraction readDecimalFractionFrom:'aa' onError:[nil] -> nil
+ "
+!
+
readFrom:aStringOrStream onError:exceptionBlock
|s numerator denominator|
@@ -1144,6 +1178,7 @@
!Fraction methodsFor:'visiting'!
acceptVisitor:aVisitor with:aParameter
+ "dispatch for visitor pattern; send #visitFraction:with: to aVisitor"
^ aVisitor visitFraction:self with:aParameter
! !
@@ -1151,11 +1186,11 @@
!Fraction class methodsFor:'documentation'!
version
- ^ '$Header: /cvs/stx/stx/libbasic/Fraction.st,v 1.82 2013-05-27 08:14:32 cg Exp $'
+ ^ '$Header: /cvs/stx/stx/libbasic/Fraction.st,v 1.84 2014-11-06 16:41:42 cg Exp $'
!
version_CVS
- ^ '$Header: /cvs/stx/stx/libbasic/Fraction.st,v 1.82 2013-05-27 08:14:32 cg Exp $'
+ ^ '$Header: /cvs/stx/stx/libbasic/Fraction.st,v 1.84 2014-11-06 16:41:42 cg Exp $'
! !