Format double type with minimum number of decimal digits

后端 未结 3 1377
猫巷女王i
猫巷女王i 2021-01-04 03:11

I need to format double type so that it has minimum two decimal digits but without limitation for maximum number of decimal digits:

5     -> \"5.00\"
5.5          


        
3条回答
  •  慢半拍i
    慢半拍i (楼主)
    2021-01-04 03:35

    You can use the 0 format specificer for non-optional digits, and # for optional digits:

    n.ToString("0.00###")
    

    This example gives you up to five decimal digits, you can add more # positions as needed.

提交回复
热议问题