Dans tutoriel nous allons voir comment traduire des nombres en lettres en Français dans C# et VB.net.
Dans un de mes projets récents, je devrais générer des factures en VB.net en utilisant CrystalReport, mais sur ces factures j’avais besoin de traduire les montants en toutes lettres en français.
Il existe plusieurs algorithmes qui permet de traduire des nombres en lettres en Français dans C# et VB.net .
Ici nous verrons comment traduire des nombres en lettres en Français dans C# et VB.net, nous ajouterons aussi l’Anglais.
Traduire des nombres en lettres en Français dans C# et VB.net
Pour traduire des nombres en lettres en Français dans C# et VB.net, nous allons commencer par créer un nouveau projet et à l’intérieur de ce projet, nous allons créer une classe « Converter ».
Le code serais disponible en VB.net et C#.
La classe Converter
C’est ce que vous voudrez copier dans votre propre projet !
Cette classe s’occupe de toute la conversion d’un nombre à sa valeur équivalente en mots en anglais ou en français.
Vraiment, la seule méthode qui doit être publique est celle nommée ConvertNumberToWords mais comme la solution de démonstration contient également des tests unitaires, toutes les méthodes sont rendues publiques.
Vous ne devez jamais appeler de méthodes autres que ConvertNumberToWords directement dans votre code.
Appelez simplement cette méthode en passant votre valeur entière que vous souhaitez convertir et la langue et une chaîne vous seront renvoyées.
Code de la classe Converter en VB.net
Option Strict On Public Enum Language English = 0 French = 1 End Enum Public Class Converter Public Shared Function ConvertNumberToWords(pValue As Integer, pLanguage As Language) As String Dim strReturn As String If pValue < 0 Then Throw New NotSupportedException("negative numbers not supported") ElseIf pValue = 0 Then strReturn = If(pLanguage = Language.English, "zero", "zéro") ElseIf pValue < 10 Then strReturn = ConvertDigitToWords(pValue, pLanguage) ElseIf pValue < 20 Then strReturn = ConvertTeensToWords(pValue, pLanguage) ElseIf pValue < 100 Then strReturn = ConvertHighTensToWords(pValue, pLanguage) ElseIf pValue < 1000 Then strReturn = ConvertBigNumberToWords(pValue, 100, "hundred", pLanguage) ElseIf pValue < 1000000 Then strReturn = ConvertBigNumberToWords(pValue, 1000, "thousand", pLanguage) ElseIf pValue < 1000000000 Then strReturn = ConvertBigNumberToWords(pValue, 1000000, "million", pLanguage) Else Throw New NotSupportedException("Number is too large!!!") End If If pLanguage = Language.French Then If strReturn.EndsWith("quatre-vingt") Then 'another French exception strReturn += "s" End If End If Return strReturn End Function Public Shared Function ConvertDigitToWords(pValue As Integer, pLanguage As Language) As String Select Case pValue Case 0 Return "" Case 1 Return If(pLanguage = Language.English, "one", "un") Case 2 Return If(pLanguage = Language.English, "two", "deux") Case 3 Return If(pLanguage = Language.English, "three", "trois") Case 4 Return If(pLanguage = Language.English, "four", "quatre") Case 5 Return If(pLanguage = Language.English, "five", "cinq") Case 6 Return "six" Case 7 Return If(pLanguage = Language.English, "seven", "sept") Case 8 Return If(pLanguage = Language.English, "eight", "huit") Case 9 Return If(pLanguage = Language.English, "nine", "neuf") Case Else Throw New IndexOutOfRangeException("{pValue} not a digit") End Select End Function 'assumes a number between 10 & 19 Public Shared Function ConvertTeensToWords(pValue As Integer, pLanguage As Language) As String Select Case pValue Case 10 Return If(pLanguage = Language.English, "ten", "dix") Case 11 Return If(pLanguage = Language.English, "eleven", "onze") Case 12 Return If(pLanguage = Language.English, "twelve", "douze") Case 13 Return If(pLanguage = Language.English, "thirteen", "treize") Case 14 Return If(pLanguage = Language.English, "fourteen", "quatorze") Case 15 Return If(pLanguage = Language.English, "fifteen", "quinze") Case 16 Return If(pLanguage = Language.English, "sixteen", "seize") Case 17 Return If(pLanguage = Language.English, "seventeen", "dix-sept") Case 18 Return If(pLanguage = Language.English, "eighteen", "dix-huit") Case 19 Return If(pLanguage = Language.English, "nineteen", "dix-neuf") Case Else Throw New IndexOutOfRangeException("{pValue} not a teen") End Select End Function 'assumes a number between 20 and 99 Public Shared Function ConvertHighTensToWords(pValue As Integer, pLanguage As Language) As String Dim tensDigit As Integer = CInt(Math.Floor(CDbl(pValue) / 10.0)) Dim tensStr As String Select Case tensDigit Case 2 tensStr = If(pLanguage = Language.English, "twenty", "vingt") Exit Select Case 3 tensStr = If(pLanguage = Language.English, "thirty", "trente") Exit Select Case 4 tensStr = If(pLanguage = Language.English, "forty", "quarante") Exit Select Case 5 tensStr = If(pLanguage = Language.English, "fifty", "cinquante") Exit Select Case 6 tensStr = If(pLanguage = Language.English, "sixty", "soixante") Exit Select Case 7 tensStr = If(pLanguage = Language.English, "seventy", "soixante-dix") Exit Select Case 8 tensStr = If(pLanguage = Language.English, "eighty", "quatre-vingt") Exit Select Case 9 tensStr = If(pLanguage = Language.English, "ninety", "quatre-vingt-dix") Exit Select Case Else Throw New IndexOutOfRangeException("{pValue} not in range 20-99") End Select If pValue Mod 10 = 0 Then Return tensStr 'French sometime has a prefix in front of 1 Dim strPrefix As String = String.Empty If pLanguage = Language.French AndAlso (tensDigit < 8) AndAlso (pValue - tensDigit * 10 = 1) Then strPrefix = "-et" End If Dim onesStr As String If pLanguage = Language.French AndAlso (tensDigit = 7 OrElse tensDigit = 9) Then tensStr = ConvertHighTensToWords(10 * (tensDigit - 1), pLanguage) onesStr = ConvertTeensToWords(10 + pValue - tensDigit * 10, pLanguage) Else onesStr = ConvertDigitToWords(pValue - tensDigit * 10, pLanguage) End If Return Convert.ToString((tensStr & strPrefix) + "-") & onesStr End Function ' Use this to convert any integer bigger than 99 Public Shared Function ConvertBigNumberToWords(pValue As Integer, baseNum As Integer, baseNumStr As String, pLanguage As Language) As String ' special case: use commas to separate portions of the number, unless we are in the hundreds Dim separator As String If pLanguage = Language.French Then separator = " " Else separator = If((baseNumStr <> "hundred"), ", ", " ") End If ' Strategy: translate the first portion of the number, then recursively translate the remaining sections. ' Step 1: strip off first portion, and convert it to string: Dim bigPart As Integer = CInt(Math.Floor(CDbl(pValue) / baseNum)) Dim bigPartStr As String If pLanguage = Language.French Then Dim baseNumStrFrench As String Select Case baseNumStr Case "hundred" baseNumStrFrench = "cent" Exit Select Case "thousand" baseNumStrFrench = "mille" Exit Select Case "million" baseNumStrFrench = "million" Exit Select Case "billion" baseNumStrFrench = "milliard" Exit Select Case Else baseNumStrFrench = "????" Exit Select End Select If bigPart = 1 AndAlso pValue < 1000000 Then bigPartStr = baseNumStrFrench Else bigPartStr = Convert.ToString(ConvertNumberToWords(bigPart, pLanguage) & Convert.ToString(" ")) & baseNumStrFrench End If Else bigPartStr = Convert.ToString(ConvertNumberToWords(bigPart, pLanguage) & Convert.ToString(" ")) & baseNumStr End If ' Step 2: check to see whether we're done: If pValue Mod baseNum = 0 Then If pLanguage = Language.French Then If bigPart > 1 Then 'in French, a s is required to cent/mille/million/milliard if there is a value in front but nothing after Return bigPartStr & Convert.ToString("s") Else Return bigPartStr End If Else Return bigPartStr End If End If ' Step 3: concatenate 1st part of string with recursively generated remainder: Dim restOfNumber As Integer = pValue - bigPart * baseNum Return Convert.ToString(bigPartStr & separator) & ConvertNumberToWords(restOfNumber, pLanguage) End Function End Class
Code de la classe Converter en C#
using System; namespace DemoNumberToWords { public enum Language { English = 0, French = 1 } public class Converter { public static string ConvertNumberToWords(int pValue, Language pLanguage) { string strReturn; if (pValue < 0) throw new NotSupportedException("negative numbers not supported"); else if (pValue == 0) strReturn=pLanguage == Language.English ? "zero" : "zéro"; else if (pValue < 10) strReturn= ConvertDigitToWords(pValue, pLanguage); else if (pValue < 20) strReturn = ConvertTeensToWords(pValue, pLanguage); else if (pValue < 100) strReturn = ConvertHighTensToWords(pValue, pLanguage); else if (pValue < 1000) strReturn = ConvertBigNumberToWords(pValue, 100, "hundred", pLanguage); else if (pValue < 1000000) strReturn = ConvertBigNumberToWords(pValue, 1000, "thousand", pLanguage); else if (pValue < 1000000000) strReturn = ConvertBigNumberToWords(pValue, 1000000, "million", pLanguage); else throw new NotSupportedException("Number is too large!!!"); if (pLanguage == Language.French) { if (strReturn.EndsWith("quatre-vingt")) { //another French exception strReturn += "s"; } } return strReturn; } public static string ConvertDigitToWords(int pValue, Language pLanguage) { switch (pValue) { case 0: return ""; case 1: return pLanguage == Language.English ? "one" : "un"; case 2: return pLanguage == Language.English ? "two" : "deux"; case 3: return pLanguage == Language.English ? "three" : "trois"; case 4: return pLanguage == Language.English ? "four" : "quatre"; case 5: return pLanguage == Language.English ? "five" : "cinq"; case 6: return "six"; case 7: return pLanguage == Language.English ? "seven" : "sept"; case 8: return pLanguage == Language.English ? "eight" : "huit"; case 9: return pLanguage == Language.English ? "nine" : "neuf"; default: throw new IndexOutOfRangeException($"{pValue} not a digit"); } } //assumes a number between 10 & 19 public static string ConvertTeensToWords(int pValue, Language pLanguage) { switch (pValue) { case 10: return pLanguage == Language.English ? "ten" : "dix"; case 11: return pLanguage == Language.English ? "eleven" : "onze"; case 12: return pLanguage == Language.English ? "twelve" : "douze"; case 13: return pLanguage == Language.English ? "thirteen" : "treize"; case 14: return pLanguage == Language.English ? "fourteen" : "quatorze"; case 15: return pLanguage == Language.English ? "fifteen" : "quinze"; case 16: return pLanguage == Language.English ? "sixteen" : "seize"; case 17: return pLanguage == Language.English ? "seventeen" : "dix-sept"; case 18: return pLanguage == Language.English ? "eighteen" : "dix-huit"; case 19: return pLanguage == Language.English ? "nineteen" : "dix-neuf"; default: throw new IndexOutOfRangeException($"{pValue} not a teen"); } } //assumes a number between 20 and 99 public static string ConvertHighTensToWords(int pValue, Language pLanguage) { int tensDigit = (int)(Math.Floor((double)pValue / 10.0)); string tensStr; switch (tensDigit) { case 2: tensStr = pLanguage == Language.English ? "twenty" : "vingt"; break; case 3: tensStr = pLanguage == Language.English ? "thirty" : "trente"; break; case 4: tensStr = pLanguage == Language.English ? "forty" : "quarante"; break; case 5: tensStr = pLanguage == Language.English ? "fifty" : "cinquante"; break; case 6: tensStr = pLanguage == Language.English ? "sixty" : "soixante"; break; case 7: tensStr = pLanguage == Language.English ? "seventy" : "soixante-dix"; break; case 8: tensStr = pLanguage == Language.English ? "eighty" : "quatre-vingt"; break; case 9: tensStr = pLanguage == Language.English ? "ninety" : "quatre-vingt-dix"; break; default: throw new IndexOutOfRangeException($"{pValue} not in range 20-99"); } if (pValue % 10 == 0) return tensStr; //French sometime has a prefix in front of 1 string strPrefix = string.Empty; if (pLanguage == Language.French && (tensDigit < 8) && (pValue - tensDigit * 10 == 1)) strPrefix = "-et"; string onesStr; if (pLanguage == Language.French && (tensDigit == 7 || tensDigit == 9)) { tensStr = ConvertHighTensToWords(10 * (tensDigit - 1), pLanguage); onesStr = ConvertTeensToWords(10 + pValue - tensDigit * 10, pLanguage); } else onesStr = ConvertDigitToWords(pValue - tensDigit * 10, pLanguage); return tensStr + strPrefix + "-" + onesStr; } // Use this to convert any integer bigger than 99 public static string ConvertBigNumberToWords(int pValue, int baseNum, string baseNumStr, Language pLanguage) { // special case: use commas to separate portions of the number, unless we are in the hundreds string separator; if (pLanguage == Language.French) separator = " "; else separator= (baseNumStr != "hundred") ? ", " : " "; // Strategy: translate the first portion of the number, then recursively translate the remaining sections. // Step 1: strip off first portion, and convert it to string: int bigPart = (int)(Math.Floor((double)pValue / baseNum)); string bigPartStr; if (pLanguage == Language.French) { string baseNumStrFrench; switch (baseNumStr) { case "hundred": baseNumStrFrench = "cent"; break; case "thousand": baseNumStrFrench = "mille"; break; case "million": baseNumStrFrench = "million"; break; case "billion": baseNumStrFrench = "milliard"; break; default: baseNumStrFrench = "????"; break; } if (bigPart == 1 && pValue < 1000000) bigPartStr = baseNumStrFrench; else bigPartStr = ConvertNumberToWords(bigPart, pLanguage) + " " + baseNumStrFrench; } else bigPartStr = ConvertNumberToWords(bigPart, pLanguage) + " " + baseNumStr; // Step 2: check to see whether we're done: if (pValue % baseNum == 0) { if (pLanguage == Language.French) { if (bigPart > 1) { //in French, a s is required to cent/mille/million/milliard if there is a value in front but nothing after return bigPartStr + "s"; } else return bigPartStr; } else return bigPartStr; } // Step 3: concatenate 1st part of string with recursively generated remainder: int restOfNumber = pValue - bigPart * baseNum; return bigPartStr + separator + ConvertNumberToWords(restOfNumber, pLanguage); } } }
Nous avons créer une classe qui classe « Translate » qui contient une énumération des langues, cela permet d’ajouter plu-tard facilement d’autres langues.
Code de la classe Translate en VB.net
Public Class Translation Public Property Value As Integer Public Property English As String Public Property French As String End Class
Code de la classe Translate en C#
public class Translation { public int Value { get; set; } public string English { get; set; } public string French { get; set; } }
Utilisation de la classe Converter
Après avoir crée la classe, nous allons voir concrètement comment traduire des nombres en lettres en Français dans C# et VB.net.
Pour ce faire, dans la méthode dans laquelle vous voulez traduire, ajoutez un code qui ressemble au suivant :
Dim t As New Translation() Dim textEnAnglais as String Dim textEnFrancais as String Dim intNombreATraduire as Integer textEnAnglais = Converter.ConvertNumberToWords(intNombreATraduire, Language.English) textEnFrancais = Converter.ConvertNumberToWords(intNombreATraduire, Language.French)
Conclusion
Dans ce tutoriel, vous avez vu comment traduire des nombres en lettres en Français dans C# et VB.net.
Leave a Comment