number to letter conversion

Just starting out? Need help? Post your questions and find answers here.
jak64
Enthusiast
Enthusiast
Posts: 639
Joined: Sat Aug 15, 2020 5:02 pm
Location: Ciboure (France)

number to letter conversion

Post 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
DarkDragon
Addict
Addict
Posts: 2347
Joined: Mon Jun 02, 2003 9:16 am
Location: Germany
Contact:

Re: number to letter conversion

Post by DarkDragon »

It is language dependent, do you need it for English only?
bye,
Daniel
jak64
Enthusiast
Enthusiast
Posts: 639
Joined: Sat Aug 15, 2020 5:02 pm
Location: Ciboure (France)

Re: number to letter conversion

Post 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
PurePilish
User
User
Posts: 10
Joined: Mon Oct 07, 2024 9:15 am

Re: number to letter conversion

Post 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.
User avatar
Mindphazer
Enthusiast
Enthusiast
Posts: 487
Joined: Mon Sep 10, 2012 10:41 am
Location: Savoie

Re: number to letter conversion

Post 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
MacBook Pro 16" M4 Pro - 24 Gb - MacOS 15.4.1 - Iphone 15 Pro Max - iPad at home
...and unfortunately... Windows at work...
AZJIO
Addict
Addict
Posts: 2224
Joined: Sun May 14, 2017 1:48 am

Re: number to letter conversion

Post by AZJIO »

Number converter
I converted the code from AutoIt3 to PureBasic
Marc56us
Addict
Addict
Posts: 1600
Joined: Sat Feb 08, 2014 3:26 pm

Re: number to letter conversion

Post by Marc56us »

User avatar
Caronte3D
Addict
Addict
Posts: 1371
Joined: Fri Jan 22, 2016 5:33 pm
Location: Some Universe

Re: number to letter conversion

Post by Caronte3D »

jak64
Enthusiast
Enthusiast
Posts: 639
Joined: Sat Aug 15, 2020 5:02 pm
Location: Ciboure (France)

Re: number to letter conversion

Post 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.
User avatar
Caronte3D
Addict
Addict
Posts: 1371
Joined: Fri Jan 22, 2016 5:33 pm
Location: Some Universe

Re: number to letter conversion

Post 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
Post Reply