Solved

precision issue in C#.NET double value.

Posted on 2013-01-20
6
377 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
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 
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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Article by: Ivo
C# And Nullable Types Since 2.0 C# has Nullable(T) Generic Structure. The idea behind is to allow value type objects to have null values just like reference types have. This concerns scenarios where not all data sources have values (like a databa…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

757 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now