Double to String (with more than 7 significant digits)

Hi,

I need to convert a Double to String retaining the double precision of the number. With Str a double is
“truncated” to 7 significant digits. Is there another option?

Thanks!

With both Format & Str you you have control of the number of digits with the format string

probably I’m missing something stupid but…a double number in general does not have a “fixed” format. how can I use Format to convert any double number to string?

dim d as double=123.4567
dim s as string
s=format(d,"###0.00000000")

but beware as has been discussed many times before, the accuracy of a double becomes suspect under certain circumstances. This is not a bug in XOJO, but rather in the way doubles are stored.

  1. decide what format you want and convert it to that
  2. use the # formatting character which is an optional placeholder
  3. use all 0’s in the pattern and trim them off either end as meets your spec
  4. invent yet another way to do this that meets the needs you have that I cant divine from here - my esp sucks :slight_smile:

I tried to use Format but a single Format cannot work in all cases: the number of significant digits is not fixed, you can also have numbers with scientific notation, etc…
Maybe a solution could be to use regex. I will investigate that.

Not sure what you really want. STR(x) will format it using a best guess, FORMAT lets you control it
but still depending on the actual value, you may NOT actually get back “exactly” what you expect

  dim d as double
  d=3910823413.1415969247
  msgbox str(d)+" "+format(d,"#0.0000000000")

returns
3.910823e+9
and
3910823413.1415967941

NOT
3910823413.1415969247

I’m curious why 7 significant digits is so pressing ?

Use Bob Delaney’s maths plugins for exact representations or user defined precision (like 100 significant digits)

[quote=239392:@Dave S]Not sure what you really want. STR(x) will format it using a best guess, FORMAT lets you control it
but still depending on the actual value, you may NOT actually get back “exactly” what you expect

  dim d as double
  d=3910823413.1415969247
  msgbox str(d)+" "+format(d,"#0.0000000000")

returns
3.910823e+9
and
3910823413.1415967941

NOT
3910823413.1415969247[/quote]

I was noticing that too and it puzzles me. Never experienced that behavior in C++.

I develop scientific software and in many applications float precision is simply not enough. That’s why I am interested in a precise representation of a double number.

Oh it exists …
There’s whole books on handlng this in C/ C++ (look up Numerical Recipes in C)

Then you want more than 7 digits
But you’ll get various small discrepancies
I’ll cite this one yet again https://en.wikipedia.org/wiki/IEEE_floating_point
And this is NOT specific to Xojo
It exists in ALL languages that use IEEE 754 formats for floating point - including C and C++

most languages use IEEE formats to store double, resulting in a maximum of 15 (I beleive) digits total precision (left AND right of decimal point)

As a test… I checked what SWIFT would do with my above example
as a double it returned 3910823413.1416
as a float it returned 3.91082e09 [CGFloat returns the same]

Double is a 64bit value, Float is a 32 bit (CGFloat is either based on iOS device type)

So I bet you DID experience this in C++ and just never noticed… -OR- you were using some special double datatype

Here is Bob’s homepage: http://delaneyrm.com

It is free, does exactly what you want (and more), and you shouldn’t write scientific software without it. Now stop fussing and get it. :stuck_out_tongue_winking_eye:

P.S. I recommend a subscription to xDev magazine too. Problems like this have been covered …

Then do NOT use floating point representation. It’s too limited.