How do I format a C# decimal to remove extra following 0's?

后端 未结 11 1848
长发绾君心
长发绾君心 2021-01-31 07:40

I want to format a string as a decimal, but the decimal contains some following zeros after the decimal. How do I format it such that those meaningless 0\'s disappear?

11条回答
  •  隐瞒了意图╮
    2021-01-31 08:14

    Somewhat hackish, but this should work:

    decimal a = 100.00M;
    string strNumber = string.Format("{0}", a);
    Console.WriteLine(strNumber.Contains('.') ? strNumber.TrimEnd('0').TrimEnd('.') : strNumber);
    

提交回复
热议问题