'From OLPC2.0 of ''24 October 2006'' [latest update: #1449] on 18 July 2007 at 4:06:05 pm'!
"Change Set: lessENotation-sw
Date: 18 July 2007
Author: Scott Wallace
Improvements for printout of numeric values in etoy readouts:
* Integer values up to fifteen digts long are fully printed in conventional notation.
* Floats also are conventionally printed unless *very* large or *very* close to 0.
In practice, this should reduce reduce the occurrence of e-notation in etoy readouts to practically nil, except where extreme values are intentionally being played with..
Cross-published from Moshi update 0249lessENotation-sw.
"!
!Number methodsFor: 'printing' stamp: 'sw 7/18/2007 16:05'!
printShowingDecimalPlaces: placesDesired
"Print the receiver showing precisely the given number of places desired. If placesDesired is positive, a decimal point and that many digits after the decimal point will always be shown. If placesDesired is zero, a whole number will be shown, without a decimal point. If the nature of the receiver is such that e-notation should be used, that is done."
| precision rounded frac sign integerString fractionString result aString aFloat decPt between myAbs |
self isNaN ifTrue: [^ 'NaN'].
aString := (aFloat := self asFloat) printString.
aFloat isInfinite ifTrue: [^ aFloat printString].
myAbs := aFloat abs.
(aString indexOf: $e ifAbsent: [nil]) ifNotNilDo:
[:ePosition | ((myAbs < 1.0e-15) or: [myAbs > 1.0e15]) ifTrue:
[decPt := aString indexOf: $. ifAbsent: [^ aString].
between := aString copyFrom: (decPt + 1) to: (ePosition - 1).
^ String streamContents: [:aStream |
aStream nextPutAll: (aString copyFrom: 1 to: decPt).
aStream nextPutAll: between.
aStream nextPutAll: (aString copyFrom: ePosition to: aString size)]]].
"The remainder of this method is courtesy of Frank Sergeant, Dec/06"
placesDesired <= 0 ifTrue: [^ self rounded printString].
precision _ Utilities floatPrecisionForDecimalPlaces: placesDesired.
rounded _ self roundTo: precision.
sign := rounded negative ifTrue: ['-'] ifFalse: [''].
integerString := rounded abs integerPart asInteger printString.
frac := ((rounded abs fractionPart roundTo: precision) * (10 raisedToInteger: placesDesired)) asInteger.
fractionString := frac printString padded: #left to: placesDesired with: $0.
result := sign , integerString , '.' , fractionString.
^ result
"
23 printShowingDecimalPlaces: 2
23.5698 printShowingDecimalPlaces: 2
-234.567 printShowingDecimalPlaces: 5
23.4567 printShowingDecimalPlaces: 0
23.5567 printShowingDecimalPlaces: 0
-23.4567 printShowingDecimalPlaces: 0
-23.5567 printShowingDecimalPlaces: 0
100000000 printShowingDecimalPlaces: 1
0.98 printShowingDecimalPlaces: 2
-0.98 printShowingDecimalPlaces: 2
2.567 printShowingDecimalPlaces: 2
-2.567 printShowingDecimalPlaces: 2
0 printShowingDecimalPlaces: 2
Float infinity printShowingDecimalPlaces: 5
2345.67890123 printShowingDecimalPlaces: 5
23456789.0012345 printShowingDecimalPlaces: 3
"! !
!Integer methodsFor: 'printing' stamp: 'sw 7/18/2007 15:50'!
printShowingDecimalPlaces: placesDesired
"Print the receiver showing the given number of decimal places."
^ (self abs > 1.0e15)
ifTrue:
[super printShowingDecimalPlaces: placesDesired]
ifFalse:
[placesDesired > 0
ifTrue:
[self printString, '.', ('' padded: #right to: placesDesired with: $0)]
ifFalse:
[self printString]]! !