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

后端 未结 11 1911
长发绾君心
长发绾君心 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:20

    double a = 1100.00
    double  b =1100.1
    double  c = 1100.100
    double  d =1100.1000
    
    Remove last zero after point
    
    
    string stra = a.toString("0.00").TrimEnd('0').TrimEnd('.');
    string strb = b.toString("0.00").TrimEnd('0').TrimEnd('.');
    string strc = c.toString("0.00").TrimEnd('0').TrimEnd('.');
    string strd = d.toString("0.00").TrimEnd('0').TrimEnd('.');
    
    Output
    
    1100
    1100.1
    1100.1
    1100.1
    

提交回复
热议问题