# Format double number of 18 digits

I’m doing an application with Xojo.
I’m using a SQLite database in wich I save a double value of 18digits.
When I get this value and I convert the number into string, the number get the exponential form.
On the reference I saw to use the Format function, but if I use this function, my number is rounden even thought I use 18 “#”.

How can I fix this problem?

Thanks to all for the answers.

If my memory is correct, a double has a maximum capability of 17 digits. I would have to do some digging to confirm this but that is the number I have gone with for a long time now.

Here is a Wikipedia article and it seems to show 16 digits while I recall that it can show 17 digits if the first digit is less than a certain number.

Double-precision floating-point format

And if I need to use a number with 18digits , how can I do?

You can get some result with this format string:

``"-#.########"``

Also look into Bob Delaney’s free fp Plugin to handle big numbers.

Using currency instead of double? This solved the exponential display issue for me. I don’t know if this is an option in your app …

Time for a little lesson on how doubles actually work.

They store numbers in what amounts to scientific notation (but with 2 instead of 10 as the base). Of the 64 bits in a double, 52 store the mantissa (also called the significand in this context), 11 store the exponent, and one stores the sign.

Consider the number 300 quadrillion (300 biliardi in Italian), which is an 18-digit number. It is slightly more than 2??. To be specific, in binary-scientific, it is 1.00001010…2??. The initial 1 is assumed in floating-point numbers, so the first bit of the significand represents the 2?? place, the second the 2?? place, and so on down, with the 52nd and final bit being the 2? place. Thus, for numbers in that size range, the smallest difference that it can store is 2? (that is, 64). So of course it gets rounded.

Ways around this include the Int64 type, which uses 63 bits for value (one for the sign) with a smallest difference of 1, and the Currency type, which does the same but with an implied divisor of 10’000. If neither of those works, you either end up having to roll your own or use a plugin (the one Kem suggests is a good one).

Guys, you are not answering the question that was asked. Since I have the same question, I will pose it too. The OP, and I, have a double that needs to be stored in an SQLite database. The number in my case is the number of seconds since the epoch, supplied as a double by `date.SecondsFrom1970`. A typical value might be 1451334053.0, and note that in my case it is an integer value (although still a double).

As the OP found out, if you use `str()` to convert that, it comes out as 1.451334e+9, which is no use at all. Instead, I have to use the format() function in an incredibly clunky way, as in:

``"update mytable set epochsecs=" + format (mydouble, "##################")``

What I want, IOW, is that this:

``msgbox (str(1451334053.0))``

should display 1451334053.0 and not 1.451334e+9.

Have you tried using Text rather than String? On Windows this code

``` Dim d As Double Dim dte As New Date d = dte.TotalSeconds Dim t As Text = d.ToText MsgBox d.ToText + EndOfLine + t ```

Results in this

Pass the optional mask to Str().

msgbox(str(1451334053.0, "###########.#))

Do not use Format or CStr to store data. They are for display.

But really, shouldn’t you be passing the number to the database directly instead of converting it to a string? Use a DatabaseRecord, RecordSet or PreparedStatement.