Click here to Skip to main content
15,888,006 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

I am getting a number which is the output of a division process.

I may get,

34
5.03
6.2
123.42

SO After formatting the string I should get two digit decimals not mattering if both decimals are zero I need to display Zero(0) rather than trimming Zero(0)

So My Expected output is shown as follows

34.00
5.03
6.20
123.42

Can anyone help to get rid of this problem
Posted

You should use string.Format like in the next example:
C#
string.Format("{0:#,0.00}", yourNumber);
 
Share this answer
 
Comments
KUMAR619 26-Nov-14 2:12am    
Yes Raul Iloc, you are Right.
Thanks for your help.
Raul Iloc 26-Nov-14 2:15am    
Welcome!
Use the following function in System.Math

Math.Round(34,2)


The output of above will be 34.00
 
Share this answer
 
v2
Comments
lukeer 26-Nov-14 1:47am    
No, it doesn't. Whether you use Math.Round(double)[^] or Math.Round(decimal)[^], they return a double, or decimal respectively.
It is up to a string formatting method to determine how many trailing zeroes an application shows.
Jignesh Khant 26-Nov-14 2:48am    
You have to pass 2 parameters ie Math.Round('Your double or decimal number','number of decimals required. eg: 1 or 2 or 3 and so on')

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