Solved

precision issue in C#.NET double value.

Posted on 2013-01-20
6
383 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Independent Software Vendors: 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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
In this video, viewers will be given step by step instructions on adjusting mouse, pointer and cursor visibility in Microsoft Windows 10. The video seeks to educate those who are struggling with the new Windows 10 Graphical User Interface. Change Cu…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…

729 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