473,395 Members | 1,412 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

ToString Conversion Issue

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!


Jul 21 '05 #1
14 3102
Neil,

I haven't experimented with this at all but perhaps Convert.ToString will
give you better results?
--
____________________
Klaus H. Probst, MVP
http://www.vbbox.com/
"Neil Guyette" <ng******@gemssensors.com> wrote in message
news:eN**************@TK2MSFTNGP09.phx.gbl...
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!



Jul 21 '05 #2
Neil,

I haven't experimented with this at all but perhaps Convert.ToString will
give you better results?
--
____________________
Klaus H. Probst, MVP
http://www.vbbox.com/
"Neil Guyette" <ng******@gemssensors.com> wrote in message
news:eN**************@TK2MSFTNGP09.phx.gbl...
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!



Jul 21 '05 #3
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.

Jul 21 '05 #4
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.

Jul 21 '05 #5
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

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Jul 21 '05 #6
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

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Jul 21 '05 #7
Neil Guyette <ng******@gemssensors.com> wrote:
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

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Jul 21 '05 #8
Neil Guyette <ng******@gemssensors.com> wrote:
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

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Jul 21 '05 #9
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.

Jul 21 '05 #10
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.

Jul 21 '05 #11


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

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Jul 21 '05 #12


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

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Jul 21 '05 #13
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.

Jul 21 '05 #14
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.

Jul 21 '05 #15

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Andre | last post by:
Hi, I have a problem with my Date conversion and can't find a way to resolve it. Here's my code : friday= DateTime.Now.AddDays((day - (day * 2 + 2))) thursday= friday.AddDays(6) friday=...
14
by: Neil Guyette | last post by:
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...
101
by: Sean | last post by:
Book I am reading says that Cstr() is best method for efficency and safety however it doesnt compare that method of the .ToString() method. Which is best. Thanks
3
by: gbgeek | last post by:
I've looked around the web a bit and didn't find much luck in locating a solution. Here the bit of code that I'm working with: strMyValue = ""; for (double d = double.MinValue; d <...
31
by: Zytan | last post by:
Everything (er, every class) in C# has ToString() which is conveniently automatically invoked when using it in Debug.WriteLine() or in a string concatenation, etc. I made a struct, and I want to...
12
by: ThunderMusic | last post by:
Hi, We have a part of our application that deals with millions of records and do some processing of them. We've achieved a pretty good performance gain by developping a custom DateTime.ToString...
3
by: Mike S | last post by:
Maybe I'm missing some fundamental unwritten law of OOP, but I was wondering why the VB.NET compiler doesn't take advantage of the fact that all .NET objects, being derived from the Object base...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.