473,382 Members | 1,442 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,382 software developers and data experts.

Problem using string.Format to show a double

Boz
Hi,

I am trying to use string.Format() to output the value of a double.

double da = 100000000000.99994;
double db = 100000000000.9994;
double dc = 100000000000.994;
double dd = 1000000000.99994;

Debug.WriteLine(string.Format("a={0:F10}", da));
Debug.WriteLine(string.Format("b={0:F10}", db));
Debug.WriteLine(string.Format("c={0:F10}", dc));
Debug.WriteLine(string.Format("d={0:F10}", dd));

give the result:
a=100000000001.0000000000
b=100000000000.9990000000
c=100000000000.9940000000
d=1000000000.9999400000

The problem is that a is being rounded to 100000000001 in formatting.

Stepping through the code, da is correctly reported by the debugger as
100000000000.99994

Is this a limitation of string.Format() ? Is there an alterative way
to output a double without rounding?

Boz
Nov 16 '05 #1
3 12410


if you are able to use (or convert) to a decimal type, it will work

this code:

decimal d = 100000000000.99994M;
System.Diagnostics.Trace.WriteLine(d.ToString());

output this
100000000000.99994

-James
"Boz" wrote:
Hi,

I am trying to use string.Format() to output the value of a double.

double da = 100000000000.99994;
double db = 100000000000.9994;
double dc = 100000000000.994;
double dd = 1000000000.99994;

Debug.WriteLine(string.Format("a={0:F10}", da));
Debug.WriteLine(string.Format("b={0:F10}", db));
Debug.WriteLine(string.Format("c={0:F10}", dc));
Debug.WriteLine(string.Format("d={0:F10}", dd));

give the result:
a=100000000001.0000000000
b=100000000000.9990000000
c=100000000000.9940000000
d=1000000000.9999400000

The problem is that a is being rounded to 100000000001 in formatting.

Stepping through the code, da is correctly reported by the debugger as
100000000000.99994

Is this a limitation of string.Format() ? Is there an alterative way
to output a double without rounding?

Boz

Nov 16 '05 #2
Well, I guess it shouldn't do it, but nonetheless it does. If you need an
exact representation of the number, use the "R" (round-trip) format. It
guarantees that parsing the string output yields exactly the same result as
the original number.

Maybe the rounding occurs because the double is guaranteed to have only 15
significant digits. In your example, event the 16th one is a 9, so the
rounding comes into place.

Jon's DecimalConverter shows exact representations of the numbers as
follows:

100000000000.99993896484375
100000000000.9994049072265625
100000000000.9940032958984375
1000000000.99994003772735595703125

As you see, the first one is already malformed at the end. The only
difference is that the debugger probably tries to recover one more
significant digit than the double.ToString() method.

HTH,
Stefan

"Boz" <jj********@yahoo.com> wrote in message
news:32**************************@posting.google.c om...
Hi,

I am trying to use string.Format() to output the value of a double.

double da = 100000000000.99994;
double db = 100000000000.9994;
double dc = 100000000000.994;
double dd = 1000000000.99994;

Debug.WriteLine(string.Format("a={0:F10}", da));
Debug.WriteLine(string.Format("b={0:F10}", db));
Debug.WriteLine(string.Format("c={0:F10}", dc));
Debug.WriteLine(string.Format("d={0:F10}", dd));

give the result:
a=100000000001.0000000000
b=100000000000.9990000000
c=100000000000.9940000000
d=1000000000.9999400000

The problem is that a is being rounded to 100000000001 in formatting.

Stepping through the code, da is correctly reported by the debugger as
100000000000.99994

Is this a limitation of string.Format() ? Is there an alterative way
to output a double without rounding?

Boz

Nov 16 '05 #3
Stefan Simek <si********@kascomp.blah.sk> wrote:
Well, I guess it shouldn't do it, but nonetheless it does. If you need an
exact representation of the number, use the "R" (round-trip) format.
Just a small note of caution - round-trip *doesn't* give the exact
decimal representation.
It guarantees that parsing the string output yields exactly the same
result as the original number.


That it does.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #4

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

Similar topics

9
by: tym | last post by:
HELP!!! I'm going round the twist with this... I have a VB6 application which is using DAO to access a database (Please - no lectures on ADO, I know what I'm doing with DAO!!) Ok, problem...
4
by: Matt | last post by:
Hi there, I'm having a weird situation when converting strings to double. Normally, the following code would work right? double MyDouble = double.Parse("20.50"); // Error at runtime Well,...
9
by: Alex | last post by:
I run the following and I get a messagebox telling me the connection is closed (OK so far) and then it crashes. The error message is: SQL Server does not exist or access denied. SQL Manager is...
15
by: Peter Afonin | last post by:
Hello, I'm struggling with the string conversion to MD5 which I've never user before. I have a string that I need to encode which looks approximately like this: ...
16
by: Dany | last post by:
Our web service was working fine until we installed .net Framework 1.1 service pack 1. Uninstalling SP1 is not an option because our largest customer says service packs marked as "critical" by...
7
by: Joe | last post by:
I've tracked the performance issue down to a single class. This class derives from CollectionBase and stores a basic value type such as string, int, double, etc... I also store the type itself...
5
by: Jason | last post by:
I am having a rounding problem in a value i am trying to display in VB.NET. If I have the following code: Dim x As Single = 2726.795 Dim y As Single = Math.Round(2726.795, 2) Dim s as String =...
5
by: Vibhesh | last post by:
I am facing problem with TimeSpan structure when DirectX is used. Following is the sample code that causes the problem: ...
1
by: stevedub | last post by:
I am having some trouble configuring my array to read from a sequential file, and then calling on that to fill an array of interests. I think I have the class set up to read the file, but when I run...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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: 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
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...

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.