decimal Debitvalue = 1156.547m; decimal DEBITAMT = Convert.ToDecimal(string.Format("{0:0.00}", Debitvalue));
I have to get only two decimal places but by using this code I am getting 1156.547. Let me know which format I have to use to display two decimal places.
2f" tells the printf method to print a floating point value (the double, x, in this case) with 2 decimal places.
We use the %. 2f format specifier to display values rounded to 2 decimal places.
Rounding is not required. For example, 5.48958123 should be printed as 5.4895 if given precision is 4. In C, there is a format specifier in C. To print 4 digits after dot, we can use 0.4f in printf().
Your question is asking to display two decimal places. Using the following String.format will help:
String.Format("{0:.##}", Debitvalue)
this will display then number with up to two decimal places(e.g. 2.10 would be shown as 2.1 ).
Use "{0:.00}", if you want always show two decimal places(e.g. 2.10 would be shown as 2.10 )
Or if you want the currency symbol displayed use the following:
String.Format("{0:C}", Debitvalue)
Use Math.Round()
for rounding to two decimal places
decimal DEBITAMT = Math.Round(1156.547m, 2);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With