[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[Help-smalltalk] How to get the similar effect like printf("%.7f")?
From: |
Lee Duhem |
Subject: |
[Help-smalltalk] How to get the similar effect like printf("%.7f")? |
Date: |
Tue, 15 Dec 2009 22:26:06 +0800 |
Hi,
I want to know how to get the similar effect like printf(%.7f")?
Here is how I did it:
Float extend [
printStringWithPrecision: anInteger [
<category: 'printing'>
| e value string decPointIndex endPosition tailingZeros
tailingZeroNumber |
e := 1.0 / (10.0 raisedTo: anInteger).
value := self roundTo: e.
string := value printString.
decPointIndex := string findFirst: [ :c | c = $. ].
tailingZeros := ''.
endPosition := decPointIndex + anInteger.
tailingZeroNumber := endPosition - string size.
tailingZeroNumber > 0 ifTrue: [
tailingZeros := String new: tailingZeroNumber withAll:
$0 ].
^(string copyFrom: 1 to: (endPosition min: string size)),
tailingZeros
]
]
But I think there is a better (shorter, elegance) way to do it.
Any suggestions?
lee
- [Help-smalltalk] How to get the similar effect like printf("%.7f")?,
Lee Duhem <=