Prevent decimal rounding when using the currency string format

I have some decimal data coming from an external service. I need to format the data to two decimal places, since it represents money, but if I use the standard C format, I round the number:

var x = 42.999m; var y = string.Format("{0:C}", x); 

This results in y containing 43.00 pounds. How can I round it to £ 42.99?

(Please note that this question does not match)

+4
source share
2 answers

If you want to use the default rounding strategy, you need to do something like:

 var x = 42.999m; var y = string.Format("{0:C}", Math.Floor(x * 100) / 100); 

Math.Floor rounded; however it does not take a few decimal places, so you need to force 2 decimal place.

+2
source

although this issue has been resolved, but I suggest you use

 var y = String.Format("{0:c3}",x); // "{0:C3}" is for -123.456 ("C3", en-US) -> ($123.456) 

From here

+1
source

All Articles