Issue with StringFormat for small negative numbers that have been rounded

随声附和 提交于 2019-12-05 18:41:43

When you say ;(#,##0.00) or {0:;(}. The value gets already rounded off to 0. Hence it does not get you the result you are expecting.

So it is better to check the sign before it is negative.

var value = -0.001M;
string decimalFormat = value > 0 ? "{0:#,##0.00}" : "({0:#,##0.00})";
Console.WriteLine(String.Format(decimalFormat, value));

Choosing between the string format based on the sign should do.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!