Click here to Skip to main content
15,881,860 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
how to add value with 2 decimal to the List?? If the value is 1.1, it should add 1.10 into the List

What I have tried:

tried Math.Round(Dobule, 2). But did not work
Posted
Updated 21-May-20 3:53am

Don't confuse a value with its string representation.
Mathematically, 1.1, 1.10, 1.100, ..., 1.10000000000 are all the same value. On presentation to the end user, you can chose to represent the number of decimals you want.
C#
double value = 1.1;
Console.WriteLine(value.ToString("F1")); // prints 1.1
Console.WriteLine(value.ToString("F2")); // prints 1.10
Console.WriteLine(value.ToString("F3")); // prints 1.100
Console.WriteLine(value.ToString("F4")); // prints 1.1000
 
Share this answer
 
v2
Comments
Maciej Los 21-May-20 8:45am    
5ed!
phil.o 21-May-20 9:05am    
Thank you Maciej :)
CPallini 21-May-20 8:52am    
Have also my 5.
phil.o 21-May-20 9:05am    
Thank you Carlo :)
kamalcodeproject 21-May-20 9:11am    
Hey,
As i mentioned, i want to add with 2 decimal to the List<double> and that needs to be bind on Drop down on UI.

I know string manipulation.
In addition to solution #1 by phil.o i'd suggest to read this: Floating Point Demystified, Part 1[^]
 
Share this answer
 
Quote:
As i mentioned, i want to add with 2 decimal to the List<double>

Your sentence means that you don't understand what is a double. As already told in Solution 1, 1.1 and 1.10 are exactly the same value.
To reduce decimals, rounding can be done.
To enforce 2 decimals, the only way is formatting while converting to string.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900