help-smalltalk
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [Help-smalltalk] How to get the similar effect like printf("%.7f")?


From: Paolo Bonzini
Subject: Re: [Help-smalltalk] How to get the similar effect like printf("%.7f")?
Date: Tue, 15 Dec 2009 15:57:06 +0100

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 <address@hidden> 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
> address@hidden
> http://lists.gnu.org/mailman/listinfo/help-smalltalk
>
>




reply via email to

[Prev in Thread] Current Thread [Next in Thread]