Format decimal value to string with leading spaces

后端 未结 4 886
鱼传尺愫
鱼传尺愫 2020-12-14 14:39

How do I format a decimal value to a string with a single digit after the comma/dot and leading spaces for values less than 100?

For example, a decimal value of

4条回答
  •  离开以前
    2020-12-14 15:06

    This pattern {0,5:###.0} should work:

    string.Format("{0,5:###.0}", 12.3456) //Output  " 12.3"
    string.Format("{0,5:###.0}", 10.011)  //Output  " 10.0" 
    string.Format("{0,5:###.0}", 123.123) //Output  "123.1"
    string.Format("{0,5:###.0}", 1.123)   //Output  "  1.1"
    string.Format("{0,5:###.0}", 1234.123)//Output "1234.1"
    

提交回复
热议问题