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.
Example: 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.
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
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.
Exactly.
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
Not on the STR page https://documentation.xojo.com/api/text/str.html
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.
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.