ToString Conversion Issue

  • Thread starter Thread starter Neil Guyette
  • Start date Start date
N

Neil Guyette

Hello,



I'm having problems figuring out why a conversion between a System.Single
type roundd when calling the object's ToString method. I'm pulling data out
of an OdbcDataReader which declares one of the columns as type Single.



Example:



OdbcDataReader odbcReader = odbcCmd.ExecuteReader();

odbcReader.Read();



// At this point the odbcReader has one row with one column, the column
value

// is 1000.08008 and the system type is System.Single. I want the value to
be cast

// to a string and have precision of 7 therefore the desired result should
be

//1000.0800800.



//I tried this but it doesn't work.

string s = odbcReader[0].ToString().ToString("F7");



//now s = 1000.0800000. it rounded



//I also tried this and yes it doesn't give me the right results

String s = Convert.ToSingle(odbcReader[0]).ToString("F7")



//s = 1000.0800000 still rounding!!



Why is the ToString() rounding the value? How do I stop it from happening
and get exactly the same value that is stored in odbcReader[0] (1000.08008).





Cheers!
 
Neil,

I haven't experimented with this at all but perhaps Convert.ToString will
give you better results?
 
Hi Neil,

Thanks for your post. I reproduce the problem on my side. To work around
the problem, I suggest you convert Single to Double before converting to
String, for example:

double val = odbcReader[0];
string s = val.ToString("F5");

In the meantime, I am contacting our Developer Team to check this issue.

Have a nice day!

Regards,

HuangTM
Microsoft Online Partner Support
MCSE/MCSD

Get Secure! -- www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 
Good Morning,

First of all thanks for everyone’s reply, this message is in response to
the last posting. I have converted it to double before and in respect
to my initial example (1000.08008) I would get 1000.0800807104 which
wasn't that value in the odbcDataReader[0] (Single Type Value
1000.08008). How did those other levels of precisions exist when cast
into double. In my database the value is 1000.08008 not
1000.0800807104. By adding odbcReader[0] to my watch the value is
listed as 1000.08008. I'm not sure how casting the value to double does
double report those other levels of precisions.

Thanks,

-Neil
 
Neil Guyette said:
First of all thanks for everyone=3Fs reply, this message is in response to
the last posting. I have converted it to double before and in respect
to my initial example (1000.08008) I would get 1000.0800807104 which
wasn't that value in the odbcDataReader[0] (Single Type Value
1000.08008). How did those other levels of precisions exist when cast
into double. In my database the value is 1000.08008 not
1000.0800807104. By adding odbcReader[0] to my watch the value is
listed as 1000.08008. I'm not sure how casting the value to double does
double report those other levels of precisions.

See http://www.pobox.com/~skeet/csharp/floatingpoint.html
 
Hi Neil,

I was told by a developer that a Single has a precision of about 7 decimal
digits (Double about 15). Anything beyond this can become garbled. You
should use Decimal if you need an exact representation of your number.

In addition, the link provided by Jon seems to be
http://www.yoda.arachsys.com/csharp/floatingpoint.html.

Hope this helps.

Regards,

HuangTM
Microsoft Online Partner Support
MCSE/MCSD

Get Secure! -- www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 
Hi,

Once again thanks for your help. The real problem was when you perform
a string cast from Single (calling tostring). It would do its own
rounding, I stop this by including "R" as a format parameter in the
ToString method. The "R" garantees the same value when casted back into
a float/single and therefore contains the exact precision level.

Thanks,

-Neil
 
Hi Neil,

Thanks a lot for sharing your resultion! I also missed the "R" number
format.

Have a nice day!

Regards,

HuangTM
Microsoft Online Partner Support
MCSE/MCSD

Get Secure! -- www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 
Back
Top