[Glass] Is there a way to print a Float with all decimals but without exponential notation?
Mariano Martinez Peck
marianopeck at gmail.com
Tue Mar 4 11:29:44 PST 2014
On Mon, Mar 3, 2014 at 3:32 PM, Martin McClure <
martin.mcclure at gemtalksystems.com> wrote:
> 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".
>
Hi Martin,
Yes, that looks a bit clunky and probably slow? I am writing reports where
I print lots of floats, so this may be important.
Also, yes, my numbers can be negative and even between 0 and 1.0. Also, not
only Float but SmallDouble as well....
Wish there could be a simple method (primitive) to print all type of
64-bits floats without the exponential notion...
For the moment I guess I will stay with the exponential :(
Thanks anyway,
--
Mariano
http://marianopeck.wordpress.com
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.gemtalksystems.com/mailman/private/glass/attachments/20140304/0d553fc6/attachment.html>
More information about the Glass
mailing list