convert to double

  • Thread starter Thread starter Gav
  • Start date Start date
G

Gav

Hi all,

I have a value in a database which is of float and in order to get my value
from my textbox into my database I am doing something along the line of

sqlparm1.value= (float)Convert.ToDouble(textbox1.Text);

however if I say put in 7.8
it saves it as 7.800000190

does anyone know why it's doing this?

Thanks
Gav
 
Because the number 7.8 cannot be accurately represented in binary. Try
using the Decimal data type instead.
 
Strange. When I execute the code below, it shows the correct result:

Dim d As Decimal = Convert.ToDecimal("7.8")
MsgBox(d.ToString)

Are you sure you're not calling any other conversions or assigning the
result to any other variable?
 
I have a value in a database which is of float and in order to get my
value
from my textbox into my database I am doing something along the line of

sqlparm1.value= (float)Convert.ToDouble(textbox1.Text);

however if I say put in 7.8
it saves it as 7.800000190

does anyone know why it's doing this?
This effect is typical for floating point operations.
This is caused by the fact that one tries to store an number with infinit
decimal digits after the comma in a limited space float, double,...Even
though the number is 7.800000..... it could be stored as 7.800000001xxx or
7.799999999xxx...

The behaviour is unpredictable and can change between 2 processor models...

You have 2 possibilitues:
Either use an accurate number without decimmal digit after the comma in it,
lik INT , but multiply the float with (e.g.) 100 and then truncate.

e.g. (float)7.8 * (int)100 = (int)780 so you store 780.
When you read back you devide by 100 to get the correct 7.8 value.

Make all you calcuations based on the fact that the number is 100 times
bigger and have no decimal values after the comma.
And only convert to the normal floating point thing if you want to visualize
the thing.

The other possibility is to use double or float to make it even more
accurate, but when you want to visualize it, format it in such a way that
the number of digits after the comma is limited. E.g. show all digits except
the last 3 after the comma.

My advice is to avoid floats at all costs unless you have to.
 
Back
Top