How to format a decimal without trailing zeros

前端 未结 3 1127
走了就别回头了
走了就别回头了 2021-01-11 13:29

I\'ve just learned that a decimal somehow remembers how much trailaing zero\'s were needed to store a number. With other words: it remembers the size of the fraction.

<
3条回答
  •  既然无缘
    2021-01-11 13:45

    just apply the Format specifier zero and will remove the trailing zeros:

    string test = (1.23M * 100M).ToString("0");
    //prints 123.
    string test2 = 123.450M.ToString(".00");
    //prints 123.45.
    string test3 = 123.450M.ToString().Trim('0');
    

提交回复
热议问题