Remove trailing zeros

后端 未结 18 1217
天命终不由人
天命终不由人 2020-11-22 11:26

I have some fields returned by a collection as

2.4200
2.0044
2.0000

I want results like

2.42
2.0044
2

I t

18条回答
  •  孤城傲影
    2020-11-22 11:47

    I found an elegant solution from http://dobrzanski.net/2009/05/14/c-decimaltostring-and-how-to-get-rid-of-trailing-zeros/

    Basically

    decimal v=2.4200M;
    
    v.ToString("#.######"); // Will return 2.42. The number of # is how many decimal digits you support.
    

提交回复
热议问题