Remove trailing zeros

后端 未结 18 983
天命终不由人
天命终不由人 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 12:12

    Depends on what your number represents and how you want to manage the values: is it a currency, do you need rounding or truncation, do you need this rounding only for display?

    If for display consider formatting the numbers are x.ToString("")

    http://msdn.microsoft.com/en-us/library/dwhawy9k.aspx and

    http://msdn.microsoft.com/en-us/library/0c899ak8.aspx

    If it is just rounding, use Math.Round overload that requires a MidPointRounding overload

    http://msdn.microsoft.com/en-us/library/ms131274.aspx)

    If you get your value from a database consider casting instead of conversion: double value = (decimal)myRecord["columnName"];

提交回复
热议问题