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

Martin McClure martin.mcclure at gemtalksystems.com
Mon Mar 3 10:32:37 PST 2014


On 03/03/2014 05:46 AM, Mariano Martinez Peck wrote:
> Hi guys.
> 
> I am exporting some data to a CSV file and I am finding some differences
> with Pharo. For my use-case, I would like to:
> 
> - Print all decimals of the float
> - do not round
> - do not show exponential notation
> 
> #greaseString (or doing self printOn: strm base: 10.) rounds the string.
> For example:
> 
> 19896.800000000003 greaseString 
> -> '19896.8'
> 
> while in Pharo I get (what I want)
> 
> 19896.800000000003 greaseString 
> -> '19896.800000000003'
> 
> 
> #asStringUsingFormat: allows me to NOT use exponential notion..but I am
> forced to define the first and second parameter as well :(
> 
> #asString shows me all decimals but it uses the exponential notion:
> 
> 19896.800000000003 asString 
> -> '1.9896800000000003E+04'
> 
> 
> So what I need is a kind of #asString but not using exponential notion.
> Is that possible? 

In order to unambiguously identify an arbitrary 64-bit Float, you need
17 significant decimal digits. So it's a bit clunky, but this should
work for most cases:

| num |
num := 19896.800000000003.
^num asStringUsingFormat: {0. 17 - (num truncated printString size). false}

--> '19896.800000000003'

This assumes that your number is >= 1.0. If your number is ever
negative, or if it's between 0 and 1, the calculation of the number of
digits to the right of the decimal place would need a bit more
complexity, since the leading character would be $- or $0, neither of
which count as a "significant decimal digit".

Regards,

-Martin


More information about the Glass mailing list