Convert double to string

S

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!
 
C

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.
 
H

Homer J Simpson

Steven said:
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)
 
S

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

Homer J Simpson

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.
 
C

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top