Comment formater les numéros Int32?
-
25-09-2019 - |
Question
Quelle est la meilleure façon d'obtenir des numéros au format Int32?
Disons que j'ai cette fonction o
:
string o(int x);
est la valeur qui o
besoin de retourner selon x
x = 0 => o = 00
x = 1 => o = 01
x = 5 => o = 05
x = 10 => o = 10
x = 31 => o = 31
x = 106 => o = 106
La solution
int lorsque x utiliser
x.ToString("00");
String.Format("{0:00}",x);
Autres conseils
string o(int x)
{
return string.Format("{0:00}", x);
}
Peut utiliser PadLeft à une largeur de remplissage total de 2 avec le caractère '0'.
string o(int x) {
return x.ToString().PadLeft(2, '0');
}
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow