tags:

views:

41

answers:

2

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(formating); // text="1,235"
    string text2 = abc2.ToString(formating); // text2=""

Please help me to correct formating so that text2="0". Thanks.

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

+1  A: 

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" 
Noel Abrahams
A: 

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
LukeH
Thanks. The best answer.
Lu Lu