It is indeed possible to modify the basic float printing method in Float to achieve this effect. Can you come out with a set of printString/printOn: methods that you'd like to have, or that Squeak and Pharo have?
Thanks, Paolo On Tue, Dec 15, 2009 at 15:26, Lee Duhem <[email protected]> wrote: > 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 mailing list > [email protected] > http://lists.gnu.org/mailman/listinfo/help-smalltalk > > _______________________________________________ help-smalltalk mailing list [email protected] http://lists.gnu.org/mailman/listinfo/help-smalltalk
