Page 1 of 1

number to letter conversion

Posted: Sat Jan 04, 2025 7:56 pm
by jak64
Good morning,
I couldn't find any code on the forum to transform a number into letters, for example:

I enter €990.30 and I get Nine hundred and ninety euros and thirty cents

Thanks to you

Re: number to letter conversion

Posted: Sat Jan 04, 2025 8:27 pm
by DarkDragon
It is language dependent, do you need it for English only?

Re: number to letter conversion

Posted: Sat Jan 04, 2025 8:32 pm
by jak64
Hello DarkDragon,
I am French and I am looking for an algorithm that I could use in PureBasic but with a French conversion

example 123,45€ gives

Cent vingt trois euros et quarante cinq centimes

Re: number to letter conversion

Posted: Sat Jan 04, 2025 9:38 pm
by PurePilish
Hi, I found this online:

https://devforum.roblox.com/t/coding-ch ... nch/500012

At the bottom of the first post, click on "Here is the answer", and then scroll to the bottom of the first post in that thread where the full code is listed. It's in the Lua language but looks easy to translate to PB. It only does numbers, not currency, but it should be easy to add that feature. Hope this helps.

Re: number to letter conversion

Posted: Sat Jan 04, 2025 10:26 pm
by Mindphazer
A little help from ChatGPT :

Code: Select all

Global Dim Unites.s(19)
Unites(0) = "zéro"
Unites(1) = "un"
Unites(2) = "deux"
Unites(3) = "trois"
Unites(4) = "quatre"
Unites(5) = "cinq"
Unites(6) = "six"
Unites(7) = "sept"
Unites(8) = "huit"
Unites(9) = "neuf"
Unites(10) = "dix"
Unites(11) = "onze"
Unites(12) = "douze"
Unites(13) = "treize"
Unites(14) = "quatorze"
Unites(15) = "quinze"
Unites(16) = "seize"
Unites(17) = "dix-sept"
Unites(18) = "dix-huit"
Unites(19) = "dix-neuf"

Global Dim Dizaines.s(10)
Dizaines(2) = "vingt"
Dizaines(3) = "trente"
Dizaines(4) = "quarante"
Dizaines(5) = "cinquante"
Dizaines(6) = "soixante"
Dizaines(7) = "soixante-dix"
Dizaines(8) = "quatre-vingt"
Dizaines(9) = "quatre-vingt-dix"

Procedure.s ConvertirEntierEnMots(Nombre)
  Protected Texte.s = ""
  
  If Nombre < 20
    Texte = Unites(Nombre)
  ElseIf Nombre < 100
    Protected Dizaine = Nombre / 10
    Protected Unite = Nombre % 10
    
    If Dizaine = 7 Or Dizaine = 9
      Texte = Dizaines(Dizaine - 1) + "-" + Unites(10 + Unite)
    Else
      Texte = Dizaines(Dizaine)
      If Unite > 0
        Texte + "-" + Unites(Unite)
      EndIf
    EndIf
  ElseIf Nombre < 1000
    Protected Centaine = Nombre / 100
    Protected Reste = Nombre % 100
    
    If Centaine = 1
      Texte = "cent"
    Else
      Texte = Unites(Centaine) + " cent"
    EndIf
    
    If Reste > 0
      Texte + " " + ConvertirEntierEnMots(Reste)
    EndIf
  ElseIf Nombre < 1000000
    Protected Mille = Nombre / 1000
  Reste = Nombre % 1000
    
    If Mille = 1
      Texte = "mille"
    Else
      Texte = ConvertirEntierEnMots(Mille) + " mille"
    EndIf
    
    If Reste > 0
      Texte + " " + ConvertirEntierEnMots(Reste)
    EndIf
  ElseIf Nombre <= 1000000
    Protected Million = Nombre / 1000000
    Reste = Nombre % 1000000
    
    Texte = ConvertirEntierEnMots(Million) + " million"
    If Reste > 0
      Texte + " " + ConvertirEntierEnMots(Reste)
    EndIf
  EndIf
  
  ProcedureReturn Texte
EndProcedure

Procedure.s ConvertirNombreEnMots(Nombre.s)
  Protected PartieEntiere.i, PartieDecimale.i
  Protected Texte.s = ""
  
  PartieEntiere = Val(StringField(Nombre, 1, "."))
  PartieDecimale = Val(StringField(Nombre, 2, "."))
  
  Texte = ConvertirEntierEnMots(PartieEntiere)
  
  If PartieDecimale > 0
    Texte + " euros et " + ConvertirEntierEnMots(PartieDecimale) + " centimes"
  Else
    Texte + " euros "
  EndIf
  
  ProcedureReturn Texte
EndProcedure

Define Nombre.s
Nombre = InputRequester("Conversion de nombres", "Entrez un nombre (jusqu'à des millions):", "1234.56")
If Nombre <> ""
  MessageRequester("Résultat", ConvertirNombreEnMots(Nombre))
EndIf

Re: number to letter conversion

Posted: Sun Jan 05, 2025 4:55 am
by AZJIO
Number converter
I converted the code from AutoIt3 to PureBasic

Re: number to letter conversion

Posted: Sun Jan 05, 2025 9:10 am
by Marc56us

Re: number to letter conversion

Posted: Sun Jan 05, 2025 12:37 pm
by Caronte3D

Re: number to letter conversion

Posted: Sun Jan 05, 2025 12:46 pm
by jak64
Hello everyone,
I tested the Mindphazer code generated by ChatGpt and it works perfectly. The one from Caronte3D is also perfect for me.

Thank you for your help.

Re: number to letter conversion

Posted: Sun Jan 05, 2025 12:54 pm
by Caronte3D
jak64 wrote: Sun Jan 05, 2025 12:46 pm ...The one from Caronte3D...
Not mine, only corrected the url posted by Marc56us :D