开发者

Number Formatting String

I want to make round a number to million. This is my code:

    string formating = "#,#,,";
    decimal abc = 1234567890m;
    decimal abc2 = 0m;
    string text = abc.ToString(form开发者_C百科ating); // text="1,235"
    string text2 = abc2.ToString(formating); // text2=""

How to correct formatting so that text2="0"?

P/S: I use C# .Net 2.0.


You could use #,0,,.

Note that the number will be rounded, similar behaviour to your original format string:

Console.WriteLine(0m.ToString("#,0,,"));             //     0
Console.WriteLine(499999m.ToString("#,0,,"));        //     0
Console.WriteLine(500000m.ToString("#,0,,"));        //     1
Console.WriteLine(1234567890m.ToString("#,0,,"));    // 1,235


Try string formating = "#,##0"

Then you can write:

    string text = (abc/1000000).ToString(formating); // text="1,235" 
    string text2 = (abc2/1000000).ToString(formating); // text2="0" 
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜