2010-10-13 5 views
0

Je veux faire un nombre à un million. Ceci est mon code:Chaîne de formatage de nombres

string formating = "#,#,,"; 
    decimal abc = 1234567890m; 
    decimal abc2 = 0m; 
    string text = abc.ToString(formating); // text="1,235" 
    string text2 = abc2.ToString(formating); // text2="" 

Comment corriger la mise en forme de telle sorte que text2="0"?

P/S: J'utilise C# .Net 2.0.

+0

Qu'est-ce que vous essayez de faire? –

+0

Veuillez corriger ma chaîne de formatage, car si abc2 = 0, text2 = "", mais je veux text2 = "0". Merci. –

Répondre

1

Vous pouvez utiliser #,0,,.

Notez que le nombre sera arrondi, un comportement similaire à votre chaîne de format original:

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 
+0

Merci. La meilleure réponse –

1

Essayez string formating = "#,##0"

Ensuite, vous pouvez écrire:

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