# How to convert a double to a string?

This should be straightforward, but…

Asuming this code:

[code]
Dim aDouble As Double = 3.1415926535897932 ^ 5.5
Dim aVariant As Variant = aDouble

Dim a As String = aDouble.ToText
Dim b As String = aVariant.StringValue
Dim c As String = CStr (aDouble)
Dim d As String = Str (aDouble)[/code]

How to put the result in a string?

This is the double value of aDouble (as viewed in debuger and needed in the string)
542.405768750564107

a: 542.405769
b: 5.4240576875056411e+2
c: 542.4058
d: 542.4058

If Xojo can put the value 542.405768750564107 in the debugger window, it should be a way to also get that same value in code isn’t it?

Use FORMAT() and specify the number of digits you want.

Well, that was correct, FORMAT did the trick.

``Dim e As String = Format (aDouble, "0.0###############")``

My logic told me that this should be the exact oposite, any convertion should give the exact representation and FORMAT used to have a rounding option with less digits.

And what said the LR ?

OK, sometimes I too forgot to read it (or fail to find the data) ;).

That’s all very well, but why should I need to? The default for double.ToText or str(double) should be to produce as many digits as are meaningful. Format should only be required for a specific rounding/layout.

[quote=435947:@Ivan Tellez]Well, that was correct, FORMAT did the trick.

``Dim e As String = Format (aDouble, "0.0###############")``

My logic told me that this should be the exact opposite, any conversion should give the exact representation and FORMAT used to have a rounding option with fewer digits.[/quote]

Well I agree with your logic - especially as the format string used above makes little sense when giving it a number like 542.405…

A Double by its very nature CANNOT give the exact representation of most fractional values. It’s up to you to control the level of rounding/truncation.

Well, that is a contradiction in your post. It is well known that doubles are not precise, but that is not the issue here.

Exactly, that is the way it should work, and that is the problem, Xojo making an arbitrary truncation by default instead of using all the meaningful digits.

[quote=451052:@Ivan Tellez]
Exactly, that is the way it should work, and that is the problem, Xojo making an arbitrary truncation by default instead of using all the meaningful digits.[/quote]

``dim d as double = 1/3``

exactly how many digits are “meaningful” ?
https://en.wikipedia.org/wiki/Significant_figures
Note [quote]Significance arithmetic is a set of approximate rules for roughly maintaining significance throughout a computation.[/quote]
Xojo cant know inherently - otherwise it should ALWAYS report all digits of a double (about 15 - 17 digits)
https://en.wikipedia.org/wiki/Double-precision_floating-point_format

In the very first line: The significant figures (also known as the significant digits) of a number are digits that carry meaning contributing to its measurement resolution. This includes all digits

incomplete quote ivan

Leading an trailing zeroes are obvious
#3 is important

[quote=451060:@Norman Palardy]Leading an trailing zeroes are obvious
#3 is important[/quote]
#3 doesn’t apply in this situation, it’s a rule used in experimental measurements.

I would think once the value is stored as a double Xojo knows nothing about the significant digits of that value anyway, as mentioned by Norman before:

This is exactly what I would expect (as someone with little programming experience/knowledge).

Julen

That should be the default for str() and .ToText. If I want different, e.g., I’ve computed a probability, so it’ll be between 0.0 and 1.0, and I want to display 4 decimal places to the user in a table, I can use Format.

So you want that for this:

```Dim aDouble As Double = 5.17 Dim d As String = str (aDouble)```
The result be: 5.16999999999999993 ?

That’s the value of aDouble with 17 digits. 