Double -> String

tomhosking

Well-known member
Joined
Aug 4, 2005
Messages
49
Programming Experience
1-3
When I convert a double to a string, it sometimes gives me weird answers. This seems to be happening because it is very small, and when it converts it to a string, it includes the E-05 bit. How can I set it to output just the numbers?

Example:
CStr(1921.256197-1921.256157)
gives:
-3.99999998990097E-05
but I want:
-0.00003999
 
Use Decimal Instead of Double

The Decimal type holds decimal numbers with a precision of 28 significant decimal digits. Its purpose is to represent and manipulate decimal numbers without the rounding errors of the Single and Double types. The Decimal type replaces Visual Basic 6's Currency type. The underlying .NET type is System.Decimal.
dim x as decimal=0.0000001

msgbox(x.tostring)

tostring function basically coverts number into human readable format

or

dim x as decimal

x=CStr(1921.256197-1921.256157)

msgbox(x)


Both will work fine


 
I'm wondering why you want to convert the values to string at all rather but calculate two decimals instead ...

Dim x as decimal = Ctype(1921.256197, decimal) - Ctype(1921.1921.256157, decimal)

or if you want to pass values from textBox controls

Dim x as decimal = Ctype(textBox1.text, decimal) - Ctype(textBox2.text, decimal)

MessageBox.Show(x)
 
Back
Top