I am trying to write a string format that rounds or fills numbers up to two decimal places, and also displays negative numbers with brackets around them. This is in the WPF application, but it applies to any use of .net StringFormat. For instance:
- 2.0 → 2.00
- -2.0 → (2.00)
- 0.01 → 0.01
- -0.01 → (0.01)
- 0.001 → 0.00
- -0.001 → (0.00)
Last, a very small negative number, rounded to zero, is a problem. I still want the brackets to indicate that it was negative.
My first version was a string format:
{0:
This does not work because it rounds the number before applying the switch positively / negatively, thereby assuming that 0.00 is not negative.
Then I tried this to split the parenthesis logic into my own formatting blocks before and after the number:
{0:;(}{0:
However, however, this is an unusual behavior compared to the original example. It works correctly for -0.01, but does not display brackets for -0.001. Somehow, the first and last tokens should pick up the rounding behavior of the number display marker in the middle.
Does anyone have any ideas on how to format this so that it works the way I want?
source share