Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

precision issue in C#.NET double value.

Posted on 2013-01-20
6
Medium Priority
?
395 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 1336 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 664 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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 1336 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

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.
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
This video shows how to quickly and easily deploy an email signature for all users in Office 365 and prevent it from being added to replies and forwards. (the resulting signature is applied on the server level in Exchange Online) The email signat…
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…
Suggested Courses

876 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