Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

.NET currency formatter: can I specify the use of banker's rounding?

Does anyone know how I can get a format string to use bankers rounding? I have been using "{0:c}" but that doesn't round the same way that bankers rounding does. The Math.Round() method does bankers rounding. I just need to be able to duplicate how it rounds using a format string.


Note: the original question was rather misleading, and answers mentioning regex derive from that.

like image 961
user21767 Avatar asked Sep 24 '08 17:09

user21767


1 Answers

Can't you simply call Math.Round() on the string input to get the behavior you want?

Instead of:

string s = string.Format("{0:c}", 12345.6789);

Do:

string s = string.Format("{0:c}", Math.Round(12345.6789));
like image 132
Zach Lute Avatar answered Sep 30 '22 02:09

Zach Lute