Double To String

Hi,

I want to convert a Double to a String. The Double may hold an integer or a decimal number. I know I can convert a Double to a String with the Str function but I can’t work out how to prevent Xojo from converting it to scientific notation. For example, a large number like 4000056655665556 gets converted to "4.000057e+15". I want it to be converted to "4000056655665556".

I see in the language reference that you can pass a formatting string to the Str function but I’m not sure what format string to use that will convert a large number (such as 4000056655665556).

Can anyone help?

If it helps, what I’m actually doing is trying to create an array of each of the digits of the double (for example 12345 would become Array(1, 2, 3, 4, 5)). Not sure if there’s actually a better way to do this than using String functions.

Format function use ?

Would Format work for you? http://documentation.xojo.com/api/text/format.html

Something like:

theString = Format( theDouble, "########################.########################" )

OK. It looks like using an arbitrary long formatting string like ####################.#################### works for most numbers but the problem will persist if the number has more digits than hash symbols used in the formatting string. I’m not sure if there’s a better solution that just accepting that there’s an arbitrary limit.

You could calculate the formatting string. Take the string and do a split on the decimal, get the length of the integer, the length of the decimal. The append that many hash symbols. I think that’d work.

Gary,

at the bottom of (all ?) entries in the LR, you get a See Also useful (usually) part.

http://documentation.xojo.com/api/text/str.html

Also, if you put .00000 instead of .#####, you will get zeroes if no other value(s) are available (try to understand). Can be useful.

Regards.

The accuracy of a Double will break down after 15 digits anyways

I’ve just spent ages tinkering with this and have realised this issue :frowning:

Here’s a rough sample that uses Hal’s suggestion to take a Double and return a String (without scientific notation):

[code]
Function DoubleToString(d As Double) As String
Dim s As String = Str(d)

If s.InStr(“e”) = 0 Then Return s

// Get the exponent, sign and coefficient
Dim e As String = s.Right(s.Len - s.InStr(“e”) - 1)
Dim sign As String = s.Mid(s.InStr(“e”) + 1, 1)
Dim coeff As String = s.Replace(“e” + sign + e, “”)

// Construct a format string to use with Xojo’s Str function that
// will correctly convert the passed double d to a decimal string
// (i.e: without scientific notation).
Dim numHashes As Integer = e.Val + 1
Dim f As String
For i As Integer = 1 to numHashes
f = f + “#”
Next i
f = f + “.”
For i As Integer = 1 to numHashes
f = f + “#”
Next i

// Now we can successfully convert the passed double to a decimal string.
Return Str(d, f)
End Function[/code]

Like Dave says though - it breaks down if the double is huge.

If you don’t mind a plugin, you can use DecimalPlugin.