Solved

precision issue in C#.NET double value.

Posted on 2013-01-20
6
380 Views
Last Modified: 2013-01-24
Hi there;

I have an issue in my C#.NET project.

I have a variable having a double value and when I debug, I can see it preserves its double value, but in console, it gives the trimmed value.

This only happens for e.g. 1.0, not 1.2 or so.

The output in the console for 1.0 is 1 where as for others like 1.2, the output is still 1.2.

The line I am using is:
 Console.WriteLine(doubleval.ToString());

So, what am i missing?

Regards.
0
Comment
Question by:jazzIIIlove
  • 3
  • 2
6 Comments
 
LVL 6

Accepted Solution

by:
esolve earned 334 total points
ID: 38798406
Use this

Console.WriteLine(String.Format("{0:N2}",doubleval));

Some more info here:
http://msdn.microsoft.com/en-us/library/kfsatb94.aspx
http://msdn.microsoft.com/en-us/library/dwhawy9k.aspx
0
 
LVL 3

Assisted Solution

by:contactnaeem
contactnaeem earned 166 total points
ID: 38798409
Use decimal format: Below line will display double with two decimals.
1.0 will be 1.00
1.2 will be 1.20
.3 will be 0.30

Console.WriteLine(doubleval.ToString("###0.00"));

you can change format as per your requirement.
0
 
LVL 12

Author Comment

by:jazzIIIlove
ID: 38801194
Hi there;

It seems pretty trivial but the problem persists:

Assume the followings are double type values:
I want to see exactly the same values written here, no additional precision, no trimming or rounding.

0.9
5.1
0.17
0.0
0.15
0.15
0.9
1.1
0.92 -> while printing, I failed in this, it simply rounds to 0.9 exactly.
0.5
0.2
1.0
1.2

Your current recommendations are not completely work in every above case.

What to do? I am needless to say using Console.WriteLine(..

Regards.
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 12

Author Comment

by:jazzIIIlove
ID: 38801874
I think my algorithm is false. I will come back.

Regards.
0
 
LVL 6

Assisted Solution

by:esolve
esolve earned 334 total points
ID: 38801935
Console.WriteLine(d.ToString("#,0.0###################"));
0
 
LVL 12

Author Closing Comment

by:jazzIIIlove
ID: 38815391
I found my algorithm is false but I am not deleting the question.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
How do I hide MVVM menu items in WPF/XAML? 1 38
Where is this file? 3 33
Problem to Office 1 45
How to force output to ascii 2 41
Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project

726 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question