Solved

precision issue in C#.NET double value.

Posted on 2013-01-20
6
379 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
Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

 
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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

Suggested Solutions

Introduction This article series is supposed to shed some light on the use of IDisposable and objects that inherit from it. In essence, a more apt title for this article would be: using (IDisposable) {}. I’m just not sure how many people would ge…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

776 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