Search code examples
c#.netformattingbankers-rounding

.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.


Solution

  • 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));