How to get the similar effect like printf("%.7f")?

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

How to get the similar effect like printf("%.7f")?

Lee Duhem
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
[hidden email]
http://lists.gnu.org/mailman/listinfo/help-smalltalk
Reply | Threaded
Open this post in threaded view
|

Re: How to get the similar effect like printf("%.7f")?

Paolo Bonzini-2
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 <[hidden email]> 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
> [hidden email]
> http://lists.gnu.org/mailman/listinfo/help-smalltalk
>
>


_______________________________________________
help-smalltalk mailing list
[hidden email]
http://lists.gnu.org/mailman/listinfo/help-smalltalk
Reply | Threaded
Open this post in threaded view
|

Re: How to get the similar effect like printf("%.7f")?

Lee Duhem
On Tue, Dec 15, 2009 at 10:57 PM, Paolo Bonzini <[hidden email]> wrote:
> 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?
>

I'll try this.

lee


_______________________________________________
help-smalltalk mailing list
[hidden email]
http://lists.gnu.org/mailman/listinfo/help-smalltalk
Reply | Threaded
Open this post in threaded view
|

Re: How to get the similar effect like printf("%.7f")?

Lee Duhem
In reply to this post by Paolo Bonzini-2
On Tue, Dec 15, 2009 at 10:57 PM, Paolo Bonzini <[hidden email]> wrote:
> 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?

Pharo have a method named Float>>printShowingDecimalPlaces:
do exactly what I want.

lee


_______________________________________________
help-smalltalk mailing list
[hidden email]
http://lists.gnu.org/mailman/listinfo/help-smalltalk