Home All Groups Group Topic Archive Search About

Convert double to string

Author
2 Mar 2006 4:21 AM
Steven
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(DblA)

The result of VarA contains value "999999999999999.00" instead of
"999999999999999.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!

Author
2 Mar 2006 5:49 AM
Cerebrus
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.WriteLine(VarA)

Regards,

Cerebrus.
Author
2 Mar 2006 6:33 AM
Steven
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?


Show quoteHide quote
"Cerebrus" <zorg***@sify.com> wrote in message
news:1141278553.857715.178580@p10g2000cwp.googlegroups.com...
> 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.WriteLine(VarA)
>
> Regards,
>
> Cerebrus.
>
Author
2 Mar 2006 6:53 AM
Homer J Simpson
"Steven" <a@a.com> wrote in message
news:uWdU%23NcPGHA.812@TK2MSFTNGP10.phx.gbl...

> 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?

Declare it as Decimal. All constants should have the D attached. All
variables should be Decimal or be converted to Decimal.
Author
2 Mar 2006 8:34 AM
Cor Ligthert [MVP]
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

Show quoteHide quote
"Steven" <a@a.com> schreef in bericht
news:uWdU%23NcPGHA.812@TK2MSFTNGP10.phx.gbl...
> 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" <zorg***@sify.com> wrote in message
> news:1141278553.857715.178580@p10g2000cwp.googlegroups.com...
>> 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.WriteLine(VarA)
>>
>> Regards,
>>
>> Cerebrus.
>>
>
>
Author
2 Mar 2006 5:51 AM
Homer J Simpson
Show quote Hide quote
"Steven" <a@a.com> wrote in message
news:ODujAEbPGHA.2012@TK2MSFTNGP14.phx.gbl...
>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(DblA)
>
> The result of VarA contains value "999999999999999.00" instead of
> "999999999999999.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?

That's a big number for a Double.

Dim DecA As Decimal = 999999999999999.12D

Dim VarA As String = CStr(DecA)

Debug.Print(VarA)