J'ai écrit une fonction dans asp.net pour convertir le nombre décimal au format texte. Cela fonctionne correctement sauf pour la partie après le point décimal. Toute aide plus tôt serait très appréciée. Ci-dessous dans mon extrait de code:Modification du nombre décimal en texte dans asp.net
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
lbl_num.Text = "";
string amt = "80434.08";
decimal num = Convert.ToDecimal(amt);
lbl_num.Text = SpellDecimal(num);
}
public static string SpellDecimal(decimal number)
{
string[] digit =
{
"", "one", "two", "three", "four", "five", "six",
"seven", "eight", "nine", "ten", "eleven", "twelve",
"thirteen", "fourteen", "fifteen", "sixteen",
"seventeen", "eighteen", "nineteen"
};
string[] baseten =
{
"", "", "twenty", "thirty", "fourty", "fifty",
"sixty", "seventy", "eighty", "ninety"
};
string[] expo =
{
"", "thousand", "million", "billion", "trillion",
"quadrillion", "quintillion"
};
StringBuilder sb = new StringBuilder();
int thousands = 0;
decimal power = 1;
if (number < 0)
{
sb.Append("minus ");
number = -number;
}
decimal n = Decimal.Truncate(number);
decimal cents = Decimal.Truncate((number - n) * 100);
if (n == Decimal.Zero)
sb.Append("zero");
for (decimal i = n; i >= 1000; i /= 1000)
{
power *= 1000;
thousands++;
}
bool sep = false;
for (decimal i = n; thousands >= 0; i %= power, thousands--, power /= 1000)
{
int j = (int)(i/power);
int k = j % 100;
int hundreds = j/100;
int tens = j % 100/10;
int ones = j % 10;
if (j == 0)
continue;
if (hundreds > 0)
{
if (sep)
sb.Append(", ");
sb.Append(digit[hundreds]);
sb.Append(" hundred");
sep = true;
}
if (k != 0)
{
if (sep)
{
sb.Append(" and ");
sep = false;
}
if (k < 20)
sb.Append(digit[k]);
else
{
sb.Append(baseten[tens]);
if (ones > 0)
{
sb.Append("-");
sb.Append(digit[ones]);
}
}
}
if (thousands > 0)
{
sb.Append(" ");
sb.Append(expo[thousands]);
sep = true;
}
}
sb.Append(" and ");
if (cents < 10) sb.Append("0");
sb.Append(cents);
sb.Append("/100");
return sb.ToString();
}
}
Il me donne le résultat suivant quatre-vingt mille, quatre cent trente-quatre ans et 08/100 au lieu de quatre-vingt mille , quatre cent trente -Quatre point zéro huit
toString() ne convertit pas la figure en mots. Je le veux en mots pas la figure juste casted comme chaîne. – user7543912
Vous pouvez remplacer ToString par vous-même –