[squeak-dev] The Trunk: Kernel-nice.688.mcz
commits at source.squeak.org
commits at source.squeak.org
Wed May 16 16:37:12 UTC 2012
Bert Freudenberg uploaded a new version of Kernel to project The Trunk:
http://source.squeak.org/trunk/Kernel-nice.688.mcz
==================== Summary ====================
Name: Kernel-nice.688
Author: nice
Time: 15 May 2012, 11:21:24.599 pm
UUID: ad83d134-2a4e-4f15-b38a-363e2da73f2b
Ancestors: Kernel-eem.687
Provides a naive #printShowingMaxDecimalPlaces: and #printOn:maxDecimalPlaces:
To be tested...
{
0.7 printShowingMaxDecimalPlaces: 3.
2.0001 printShowingMaxDecimalPlaces: 3.
-1.9994 printShowingMaxDecimalPlaces: 3.
}
=============== Diff against Kernel-eem.687 ===============
Item was added:
+ ----- Method: Integer>>printOn:maxDecimalPlaces: (in category 'printing') -----
+ printOn: aStream maxDecimalPlaces: placesDesired
+ ^self printOn: aStream!
Item was added:
+ ----- Method: Number>>printOn:maxDecimalPlaces: (in category 'printing') -----
+ printOn: aStream maxDecimalPlaces: placesDesired
+ "Print a representation of the receiver on aStream in decimal notation with at most prescribed number of places after decimal separator."
+
+ | rounder rounded roundedFractionPart placesRequired shorten |
+ placesDesired <= 0 ifTrue: [^ self rounded printOn: aStream].
+ rounder := 10 raisedToInteger: placesDesired.
+ rounded := self roundTo: rounder reciprocal.
+ rounded negative ifTrue: [aStream nextPut: $-].
+ rounded := rounded abs.
+ rounded integerPart truncated printOn: aStream.
+ roundedFractionPart := (rounded fractionPart * rounder) truncated.
+ placesRequired := placesDesired.
+ [roundedFractionPart = 0 ifTrue: [^self].
+ (shorten := roundedFractionPart // 10) * 10 = roundedFractionPart]
+ whileTrue:
+ [placesRequired := placesRequired - 1.
+ roundedFractionPart := shorten].
+ placesRequired > 0
+ ifTrue:
+ [aStream nextPut: $..
+ roundedFractionPart printOn: aStream base: 10 length: placesRequired padded: true]!
Item was added:
+ ----- Method: Number>>printShowingMaxDecimalPlaces: (in category 'printing') -----
+ printShowingMaxDecimalPlaces: placesDesired
+ "Print the receiver showing at most the given number of places desired after the decimal point.
+ Trailing zeros of decimal part are discarded, so the number of digits after the decimal point may vary.
+ When placesDesired is zero or negative, or when fraction part vanish, no decimal point is shown."
+
+ ^String new: placesDesired + 10 streamContents: [:aStream |
+ self printOn: aStream maxDecimalPlaces: placesDesired]!
More information about the Squeak-dev
mailing list
|