Convert double to string

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Steven

    Convert double to string

    I have tried to use FormatNumber() to convert a very large double type
    number to String format, however, the result seems not correct, is there any
    limitation for using FormatNumber() function?

    For example, I write the code shown below:
    Dim DblA as double = 999999999999999 .12
    Dim VarA As String = FormatNumber(Db lA)

    The result of VarA contains value "99999999999999 9.00" instead of
    "99999999999999 9.12".

    I have also tried to use CStr(), It's also not work.

    So I want to ask if there any method to convert a large double number to
    string format?

    Thanks!


  • Cerebrus

    #2
    Re: Convert double to string

    Hi Steven,

    It sure works if you use a Decimal data type instead of Double. Another
    point is that you need to force the value to be of that particular
    type, by using a character literal (R for Double and D for Decimal)

    Dim DblA As Decimal = 999999999999999 .12D
    Dim VarA As String = CStr(DblA)
    Console.WriteLi ne(VarA)

    Regards,

    Cerebrus.

    Comment

    • Homer J Simpson

      #3
      Re: Convert double to string


      "Steven" <a@a.com> wrote in message
      news:ODujAEbPGH A.2012@TK2MSFTN GP14.phx.gbl...[color=blue]
      >I have tried to use FormatNumber() to convert a very large double type
      >number to String format, however, the result seems not correct, is there
      >any limitation for using FormatNumber() function?
      >
      > For example, I write the code shown below:
      > Dim DblA as double = 999999999999999 .12
      > Dim VarA As String = FormatNumber(Db lA)
      >
      > The result of VarA contains value "99999999999999 9.00" instead of
      > "99999999999999 9.12".
      >
      > I have also tried to use CStr(), It's also not work.
      >
      > So I want to ask if there any method to convert a large double number to
      > string format?[/color]

      That's a big number for a Double.

      Dim DecA As Decimal = 999999999999999 .12D

      Dim VarA As String = CStr(DecA)

      Debug.Print(Var A)






      Comment

      • Steven

        #4
        Re: Convert double to string

        What if the large decimal value is not a static value but calcuated by a
        complex formula instead?

        How can I force the output value to be that particular type just like adding
        a "D" at the end of the static value?


        "Cerebrus" <zorg007@sify.c om> wrote in message
        news:1141278553 .857715.178580@ p10g2000cwp.goo glegroups.com.. .[color=blue]
        > Hi Steven,
        >
        > It sure works if you use a Decimal data type instead of Double. Another
        > point is that you need to force the value to be of that particular
        > type, by using a character literal (R for Double and D for Decimal)
        >
        > Dim DblA As Decimal = 999999999999999 .12D
        > Dim VarA As String = CStr(DblA)
        > Console.WriteLi ne(VarA)
        >
        > Regards,
        >
        > Cerebrus.
        >[/color]


        Comment

        • Homer J Simpson

          #5
          Re: Convert double to string


          "Steven" <a@a.com> wrote in message
          news:uWdU%23NcP GHA.812@TK2MSFT NGP10.phx.gbl.. .
          [color=blue]
          > What if the large decimal value is not a static value but calcuated by a
          > complex formula instead?
          >
          > How can I force the output value to be that particular type just like
          > adding a "D" at the end of the static value?[/color]

          Declare it as Decimal. All constants should have the D attached. All
          variables should be Decimal or be converted to Decimal.



          Comment

          • Cor Ligthert [MVP]

            #6
            Re: Convert double to string

            Steven,

            You can use the very strong Visual Basic convert methods one of those is

            CDec(1234*1234)

            Be aware that Net 1.x uses the seldom used Bankers Rounding

            I hope this helps,

            Cor

            "Steven" <a@a.com> schreef in bericht
            news:uWdU%23NcP GHA.812@TK2MSFT NGP10.phx.gbl.. .[color=blue]
            > What if the large decimal value is not a static value but calcuated by a
            > complex formula instead?
            >
            > How can I force the output value to be that particular type just like
            > adding a "D" at the end of the static value?
            >
            >
            > "Cerebrus" <zorg007@sify.c om> wrote in message
            > news:1141278553 .857715.178580@ p10g2000cwp.goo glegroups.com.. .[color=green]
            >> Hi Steven,
            >>
            >> It sure works if you use a Decimal data type instead of Double. Another
            >> point is that you need to force the value to be of that particular
            >> type, by using a character literal (R for Double and D for Decimal)
            >>
            >> Dim DblA As Decimal = 999999999999999 .12D
            >> Dim VarA As String = CStr(DblA)
            >> Console.WriteLi ne(VarA)
            >>
            >> Regards,
            >>
            >> Cerebrus.
            >>[/color]
            >
            >[/color]


            Comment

            Working...