473,408 Members | 2,477 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,408 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 3103
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: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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:
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
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.