[Glass] Is there a way to print a Float with all decimals but without exponential notation?

Martin McClure martin.mcclure at gemtalksystems.com
Tue Mar 4 12:18:33 PST 2014


On 03/04/2014 12:12 PM, Mariano Martinez Peck wrote:
>
>     But how slow is slow? How many is "lots" and how often do you need
>     to print them? When it comes to performance, assumptions are very
>     risky. I recommend trying it and testing what the performance is,
>     rather than assuming it will be too slow. You can probably do that
>     kind of test in half an hour or so.
>
>
> Yes, probably I am exaggerating ;)
>
>     SmallDouble and Float should be completely polymorphic (at least
>     plenty polymorphic for this job), so you shouldn't have to worry
>     about the difference. The handling of negative and 0 - 1 won't add
>     much complexity.
>
>
> OK...so let's try implementing that method. Is it as simple as this or I
> am missing something?
>
> Float >> asStringWithAllDecimalsAndNoExponentialNotation
> | truncatedStringSize |
> truncatedStringSize := self truncated printString size.
> (self >= 1.0)
> ifTrue: [ ^ self asStringUsingFormat: {0. 17 - truncatedStringSize. false} ]
> ifFalse: [ ^ self asStringUsingFormat: {0. 18 - truncatedStringSize.
> false} ]

Yes, I think it is that simple. Though I'd write a few test cases using 
numbers between 0 and 1, -0 and -1, and of larger absolute values, just 
to be sure.

Regards,

-Martin


More information about the Glass mailing list