C# double formatting align on decimal sign

前端 未结 2 2090
萌比男神i
萌比男神i 2021-01-05 19:36

I align numbers with various number of decimals so that the decimal sign aligns on a straight row. This can be achevied by padding with spaces, but I\'m having trouble.

2条回答
  •  佛祖请我去吃肉
    2021-01-05 20:17

    You can use string.format or ToString method of double to do so.

    double MyPos = 19.95, MyNeg = -19.95, MyZero = 0.0;
    
    string MyString = MyPos.ToString("$#,##0.00;($#,##0.00);Zero");
    
    // In the U.S. English culture, MyString has the value: $19.95.
    
    MyString = MyNeg.ToString("$#,##0.00;($#,##0.00);Zero");
    
    // In the U.S. English culture, MyString has the value: ($19.95).
    // The minus sign is omitted by default.
    
    MyString = MyZero.ToString("$#,##0.00;($#,##0.00);Zero");
    
    // In the U.S. English culture, MyString has the value: Zero.

    this article from msdn can help you if you need more details

提交回复
热议问题