Click here to Skip to main content
15,895,370 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
hi..

i want to get two digit after decimal...i did it...but if the number is in round fig,in that case i also want 00 after decimal...how to do this??
Posted

Formatting is pretty simple: you just provide a parameter to the ToString (or the parameter number in string.Format / stream.Write operations):
C#
double d = 123.4567;
double z = 123.0000;
Console.WriteLine(d.ToString("0.00"));
Console.WriteLine(z.ToString("0.00"));
Console.WriteLine("{0:N2}", d);
Console.WriteLine("{0:N2}", z);
Will generate:
123.46
123.00
123.46
123.00
 
Share this answer
 
Comments
rajgaikwad01 17-Dec-13 2:40am    
how you want the result?
if you add 123.456 then you want 123.45 or only 45
and same for rounded digit like 123 then you want 123.00 or only 00
Member 10276220 17-Dec-13 2:47am    
if i get 123,then iy will convert into 123.00 or if i get 123.456,then it will convert into 123.46
Karthik_Mahalingam 17-Dec-13 3:04am    
Griff answer is right...
Please try this

Convert.ToDecimal(SALES_AMT).ToString("#,##0.00;-$#,##0.00;Zero")
 
Share this answer
 
Comments
Member 10276220 17-Dec-13 2:40am    
suppose i get the result with 123...but it converts to 123.00...if i get 123.458,then it converts to 123.46...i want just like this..
you can use math.round method
like:-
inputValue = Math.Round(inputValue, 2);
 
Share this answer
 
Try this link, you will get tons of options you can choose.

c-sharp-how-do-i-round-a-decimal-value-to-2-decimal-places[^]
 
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