How to convert a double to a string?

  1. 3 months ago

    Ivan T

    May 12 Pre-Release Testers
    Edited 3 months ago

    This should be straightforward, but...

    Asuming this 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)

    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?

    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.

  2. Tim H

    May 12 Pre-Release Testers Portland, OR USA

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

  3. Ivan T

    May 12 Pre-Release Testers Answer

    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.

  4. Emile S

    May 12 Europe (France, Strasbourg)

    And what said the LR ?

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

  5. yesterday

    Tim S

    yesterday Canterbury, UK

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

    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.

  6. Tim S

    yesterday Canterbury, UK

    @Ivan T 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.

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

  7. 16 hours ago

    Tim H

    16 hours ago Pre-Release Testers Portland, OR USA

    @Ivan T My logic told me that this should be the exact oposite, any convertion should give the exact representation

    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.

  8. 6 hours ago

or Sign Up to reply!