Login or Sign Up to become a member!
LessThanDot Sit Logo

LessThanDot

Community Wiki

Less Than Dot is a community of passionate IT professionals and enthusiasts dedicated to sharing technical knowledge, experience, and assistance. Inside you will find reference materials, interesting technical discussions, and expert tips and commentary. Once you register for an account you will have immediate access to the forums and all past articles and commentaries.

LTD Social Sitings

Lessthandot twitter Lessthandot Linkedin Lessthandot friendfeed Lessthandot facebook Lessthandot rss

Note: Watch for social icons on posts by your favorite authors to follow their postings on these and other social sites.

Navigation

Google Ads

VB.Net: Numbers to French words

From Wiki

Jump to: navigation, search
  1. Namespace Functions
  2.     ''' <summary>
  3.     '''
  4.     ''' </summary>
  5.     ''' <remarks></remarks>
  6.     Public Class NumberToFrenchWords
  7.  
  8. #Region " Private members "
  9.         ''' <summary>
  10.         '''
  11.         ''' </summary>
  12.         ''' <remarks></remarks>
  13.         Dim _Value As Double
  14.         ''' <summary>
  15.         '''
  16.         ''' </summary>
  17.         ''' <remarks></remarks>
  18.         Dim _French As String
  19. #End Region
  20.  
  21. #Region " Public properties "
  22.         ''' <summary>
  23.         '''
  24.         ''' </summary>
  25.         ''' <value></value>
  26.         ''' <returns></returns>
  27.         ''' <remarks></remarks>
  28.         Public Property Value() As Double
  29.             Get
  30.                 Return _Value
  31.             End Get
  32.             Set(ByVal Value As Double)
  33.                 _Value = Value
  34.                 NumbertoWords()
  35.             End Set
  36.         End Property
  37.  
  38.         ''' <summary>
  39.         '''
  40.         ''' </summary>
  41.         ''' <value></value>
  42.         ''' <returns></returns>
  43.         ''' <remarks></remarks>
  44.         Public ReadOnly Property French() As String
  45.             Get
  46.                 Return _French
  47.             End Get
  48.         End Property
  49. #End Region
  50.  
  51. #Region " Constructors "
  52.         ''' <summary>
  53.         '''
  54.         ''' </summary>
  55.         ''' <remarks></remarks>
  56.         Public Sub New()
  57.             _Value = 0
  58.             NumbertoWords()
  59.         End Sub
  60.  
  61.         ''' <summary>
  62.         '''
  63.         ''' </summary>
  64.         ''' <param name="Value"></param>
  65.         ''' <remarks></remarks>
  66.         Public Sub New(ByVal Value As Double)
  67.             _Value = Value
  68.             NumbertoWords()
  69.         End Sub
  70. #End Region
  71.  
  72. #Region " Private methods "
  73.         ''' <summary>
  74.         '''
  75.         ''' </summary>
  76.         ''' <remarks></remarks>
  77.         Private Sub NumbertoWords()
  78.             Dim strInwords As String
  79.             Dim dblNumberbeforecomma, dblNumberdecimals As Double
  80.             Dim intNumber(10) As Integer
  81.             Dim intTemp As Integer
  82.  
  83.             If _Value < 1000000 Then
  84.                 strInwords = ""
  85.                 If dblNumberbeforecomma >= 1000 Then
  86.                     If dblNumberbeforecomma >= 2000 Then
  87.                         strInwords = valeur(intNumber(1), intNumber(2), intNumber(3)).TrimStart(Convert.ToChar(" ")) & " mille"
  88.                     Else
  89.                         strInwords = "mille"
  90.                     End If
  91.                 End If
  92.                 If dblNumberbeforecomma > 1 Then
  93.                     strInwords &= " " & valeur(intNumber(4), intNumber(5), intNumber(6)).TrimStart(Convert.ToChar(" ")) & " euros"
  94.                 Else
  95.                     strInwords &= " " & valeur(intNumber(4), intNumber(5), intNumber(6)).TrimStart(Convert.ToChar(" ")) & " euro"
  96.                 End If
  97.                 If dblNumberdecimals > 0 Then
  98.                     If dblNumberdecimals > 1 Then
  99.                         strInwords &= " " & valeur(0, Convert.ToInt32(dblNumberdecimals) \ 10, Convert.ToInt32(dblNumberdecimals Mod 10)).TrimStart(Convert.ToChar(" ")) & " cents"
  100.                     Else
  101.                         strInwords &= " " & valeur(0, Convert.ToInt32(dblNumberdecimals) \ 10, Convert.ToInt32(dblNumberdecimals Mod 10)).TrimStart(Convert.ToChar(" ")) & " cent"
  102.                     End If
  103.                 End If
  104.                 _French = strInwords.TrimStart(Convert.ToChar(" "))
  105.             End If
  106.         End Sub
  107.  
  108.         ''' <summary>
  109.         '''
  110.         ''' </summary>
  111.         ''' <param name="number"></param>
  112.         ''' <returns></returns>
  113.         ''' <remarks></remarks>
  114.         Private Shared Function unité(ByVal number As Integer) As String
  115.             Dim A(20) As String
  116.  
  117.             A(1) = "un"
  118.             A(2) = "deux"
  119.             A(3) = "trois"
  120.             A(4) = "quatre"
  121.             A(5) = "cinq"
  122.             A(6) = "six"
  123.             A(7) = "sept"
  124.             A(8) = "huit"
  125.             A(9) = "neuf"
  126.             A(11) = "onze"
  127.             A(12) = "douze"
  128.             A(13) = "treize"
  129.             A(14) = "quatorze"
  130.             A(15) = "quinze"
  131.             A(16) = "seize"
  132.             A(17) = "dix-sept"
  133.             A(18) = "dix-huit"
  134.             A(19) = "dix-neuf"
  135.  
  136.             Return A(number)
  137.         End Function
  138.  
  139.  
  140.         ''' <summary>
  141.         '''
  142.         ''' </summary>
  143.         ''' <param name="number"></param>
  144.         ''' <returns></returns>
  145.         ''' <remarks></remarks>
  146.         Private Shared Function dizaine(ByVal number As Integer) As String
  147.             Dim A(10) As String
  148.  
  149.             A(1) = "dix"
  150.             A(2) = "vingt"
  151.             A(3) = "trente"
  152.             A(4) = "quarante"
  153.             A(5) = "cinquante"
  154.             A(6) = "soixante"
  155.             A(7) = "soixante"
  156.             A(8) = "quatre-vingt"
  157.             A(9) = "nonante"
  158.  
  159.             Return A(number)
  160.         End Function
  161.  
  162.         ''' <summary>
  163.         '''
  164.         ''' </summary>
  165.         ''' <param name="A"></param>
  166.         ''' <param name="B"></param>
  167.         ''' <param name="C"></param>
  168.         ''' <returns></returns>
  169.         ''' <remarks></remarks>
  170.         Private Shared Function valeur(ByVal A As Integer, ByVal B As Integer, ByVal C As Integer) As String
  171.             Dim spatieA, spatieB As String
  172.  
  173.             If B > 0 Then
  174.                 spatieA = " "
  175.             Else
  176.                 spatieA = ""
  177.             End If
  178.             If C > 0 Then
  179.                 spatieB = " "
  180.             Else
  181.                 spatieB = ""
  182.             End If
  183.             valeur = ""
  184.  
  185.             If A > 0 Then
  186.                 If A > 1 Then
  187.                     valeur = unité(A)
  188.                 End If
  189.                 valeur &= " cent"
  190.             End If
  191.  
  192.             If (A > 1 And B = 0 And C = 0) Then
  193.                 valeur = valeur & "s"
  194.             End If
  195.  
  196.             If B > 0 Then
  197.                 If B = 1 Then
  198.                     If C = 0 Then
  199.                         valeur = valeur & " dix"
  200.                     Else
  201.                         valeur = valeur & spatieB & unité(10 + C)
  202.                     End If
  203.                 Else
  204.                     valeur = valeur & spatieB & dizaine(B)
  205.                     If C = 0 Then
  206.                         If B = 7 Then valeur = valeur & "-dix"
  207.                         If B = 8 Then valeur = valeur & "s"
  208.                     Else
  209.                         If C = 1 Then
  210.                             If B = 7 Then valeur = valeur & " et onze"
  211.                             If B = 8 Then valeur = valeur & "-un"
  212.                             If Not (B = 7 Or B = 8) Then valeur = valeur & " et un"
  213.                         Else
  214.                             If B = 7 Then
  215.                                 valeur = valeur & "-" & unité(10 + C)
  216.                             Else
  217.                                 valeur = valeur & "-" & unité(C)
  218.                             End If
  219.                         End If
  220.                     End If
  221.                 End If
  222.             Else
  223.                 If C > 0 Then valeur = valeur & spatieB & unité(C)
  224.             End If
  225.         End Function
  226. #End Region
  227.  
  228.     End Class
  229. End Namespace

This class does what it says it does it translates a number in to words for the french language.

384 Rating: 3.0/5 (4 votes cast)