Double to string conversion truncates decimals, why?

I am using Xojo 2019r1.1

I have a Double value like this:
dim dbVal as Double = 0.0154789411233

if I convert it to a string it loses the decimals
dim sVal as string = dbVal.toText

sVal is equal to “0.015479”

How can i have a string equal to the decimal value?

Using .ToText for string is a bad thing.


dim dbVal As Double = 0.0154789411233
dim sVal As String = dbVal.ToString("0.##########")  // 10 decimals if avail. change # to 0 to require a 0.

He’s using 2019r1 Derk. ToString wasn’t there yet. Text will auto convert to a String in his case.

1 Like

In desktop ? huh? We are way ahead… in changes these days.

Well than he should do < 2019 R2

dim dbVal As Double = 0.0154789411233
dim sVal As String = Format(dbVal, "0.##########")  // 10 decimals if avail. change # to 0 to require a 0

The default is only 6 decimal places if you don’t provide a format.
You need to provide format like:

dim sVal as string = dbVal.toText(xojo.core.Locale.Current, "0.#############")

I use Xojo Desktop 2019r1.1
But I don’t know how many decimals I need, it could be even more than 10.

you could add more than 10…

No, because if I put an exaggerated number of #, for example 20 #, and then the double value is only 0.1234 the format function will put some numbers at random and it’s not good.

dim sVal As String = Format(dbVal, "0.####################")

sVal becomes “0.123450000000985787566”

I don’t know the value of dVal and it could be with few or many decimals.

use fp plugin from @Robert_Delaney
supports an infinite number of decimals

1 Like

Tha is NOT the format function, NOR a random numbers. That is how the Double works. It cant save the exact value on all the cases and format is just showing the actual value. You should be aware of the Double inner works before start using it for calculations.

Xojo is limited and doses not have a data type to store finite decimal values other than the Currency which is limited to 4 decimal digits.

So, as an alternative you can use the fp plugin as recomended above or maybe the DecimalPlugin

MBS plugins also have a BigNumberMBS

Ciao Gabriele,
what you are seeing is a representation problem.
Decimal number cannot be exactly represented as binary and this representation is shown approximated as string.
There is no solution for this and it’s the same for all platform or language unless you use a special type as suggested.
The “string” aproximation is very low and if is not low for your current problem probably you should change scale or use a special type.

But, in any case, if you convert your string as double you will get your original number.

While we are here, I was caught out by API 1 this week.

What would YOU expect

dim a as double
a = -3
dim strval as string
strval = str(a,"0.0")

to return?

(dont run it, just guess what strval should contain)
Its been the cause of a bug in my code for 18 months…

3.0 because your format doesn’t have a - sign or negative format. See Format.

But I wasnt using Format, and the docs for STR don’t mention it.

Already fixed, and if I omit the “0.0” , it works as expected.
But it was a surprise.

You weren’t using Format but you were using a format. And the doc for Str does say:

The absolute value of the number is displayed. You must use the + or - signs if you want the sign displayed.

and Format is referenced a lot within the doc page.

1 Like

That’s not part of the page about Str, and it should have been.
Don’t sweat it, I have sorted it.
Just saying, it’s not the result I would have expected. YVMV

1 Like

Errm, that’s where I got it from :thinking:

that’s where I got it from

Not on the STR page
You must have clicked on a link to the page for Format.
I just wanted to mention it in case it caught anyone else out.
Its good that you already knew.

Huh - yes, you’re right. My mistake, sorry.

Since no one has brought this up yet - if you use Variant’s StringValue function you often get the best possible precision. Not sure if that still (or ever) holds for Doubles, but it did for Integers.

So, try this:

dim v as Variant = dbVal
dim s as String = v.StringValue

That would give 1.54789411233e-2 in my test. That gives you the full precision, although it’s using exponential format. You can also stuff that back into a Variant and get the full double back from it.
Works also with Int64 values.