Write Decimal Number in words - UDF

Juggler_IN

Active Member
Joined
Nov 19, 2014
Messages
358
Office Version
  1. 2003 or older
Platform
  1. Windows
I have a UDF to write whole numbers in words. But, is there a way to write decimal number in words?

For example -

25.578 - is written as twenty-five and five hundred seventy-eight thousandths

7000.14 - is read seven thousand and fourteen hundredths

0.002 - is read two thousandths (there is no need to say zero and two thousandths)

250.00035 - is read two hundred fifty and thirty five hundred-thousandths.
 

Excel Facts

Why does 9 mean SUM in SUBTOTAL?
It is because Sum is the 9th alphabetically in Average, Count, CountA, Max, Min, Product, StDev.S, StDev.P, Sum, VAR.S, VAR.P.
By dissecting the SpellNumber UDF, you can achive with some modifications.

Inspiration from Kenneth Hobson's post #9 : https://www.mrexcel.com/forum/excel-questions/332887-spell-number-not-currency.html

Code:
Function SpellNumber(MyNumber, Optional bMoney = False)

  Dim Dollars, Cents, Temp
  Dim DecimalPlace, Count
  Dim iNumber
  Dim sFract$, iCents
  
  ReDim Place(9) As String
  Place(2) = " Thousand "
  Place(3) = " Million "
  Place(4) = " Billion "
  Place(5) = " Trillion "
  
  iNumber = MyNumber
  ' String representation of amount.
  MyNumber = Trim(Str(MyNumber))
  
  ' Position of decimal place 0 if none.
  DecimalPlace = InStr(MyNumber, ".")
  ' Convert cents and set MyNumber to dollar amount.
  If DecimalPlace > 0 Then
    iCents = Mid(MyNumber, DecimalPlace + 1)
    sFract = GetFract(iCents)
    Cents = GetHundreds(iCents)
    MyNumber = Trim(Left(MyNumber, DecimalPlace - 1))
  End If
  
  Count = 1
  Do While MyNumber <> ""
    Temp = GetHundreds(Right(MyNumber, 3))
    If Temp <> "" Then Dollars = Temp & Place(Count) & Dollars
    If Len(MyNumber) > 3 Then
      MyNumber = Left(MyNumber, Len(MyNumber) - 3)
      Else
      MyNumber = ""
    End If
    Count = Count + 1
  Loop
  
  If bMoney = True Then
    Select Case Dollars
      Case ""
        Dollars = "No Dollars"
      Case "One"
        Dollars = "One Dollar"
      Case Else
       Dollars = Dollars & " Dollars"
    End Select
    
    Select Case Cents
      Case ""
       Cents = " and No Cents"
      Case "One"
        Cents = " and One Cent"
      Case Else
        Cents = " and " & Cents & " Cents"
    End Select
    SpellNumber = Dollars & Cents
    Exit Function
  End If
  
  If iNumber <> CInt(iNumber) Then
    If Len(Dollars) > 0 Then
      SpellNumber = Dollars & " and " & Cents & sFract
    Else
      SpellNumber = Cents & sFract
    End If
    Exit Function
  End If
  SpellNumber = Dollars
End Function


Function GetFract(ByVal sFract)
Select Case Len(sFract)
    Case 1
        GetFract = " Tenths"
    Case 2
        GetFract = " Hundredths"
    Case 3
        GetFract = " Thousandths"
    Case 4
        GetFract = " Ten Thousandths"
    Case 5
        GetFract = " Hundred Thousandths"
    Case 6
        GetFract = " Millionths"
End Select
        
End Function




'*******************************************
' Converts a number from 100-999 into text *
'*******************************************


Function GetHundreds(ByVal MyNumber)
  Dim Result As String
  
  If Val(MyNumber) = 0 Then Exit Function
  MyNumber = Right("000" & MyNumber, 3)
  
  ' Convert the hundreds place.
  If Mid(MyNumber, 1, 1) <> "0" Then
    Result = GetDigit(Mid(MyNumber, 1, 1)) & " Hundred "
  End If
  
  ' Convert the tens and ones place.
  If Mid(MyNumber, 2, 1) <> "0" Then
   Result = Result & GetTens(Mid(MyNumber, 2))
    Else
    Result = Result & GetDigit(Mid(MyNumber, 3))
  End If
  
  GetHundreds = Result
End Function






'*********************************************
' Converts a number from 10 to 99 into text. *
'*********************************************


Function GetTens(TensText)
  Dim Result As String
  
  Result = "" ' Null out the temporary function value.
  If Val(Left(TensText, 1)) = 1 Then ' If value between 10-19...
    Select Case Val(TensText)
      Case 10: Result = "Ten"
      Case 11: Result = "Eleven"
      Case 12: Result = "Twelve"
      Case 13: Result = "Thirteen"
      Case 14: Result = "Fourteen"
      Case 15: Result = "Fifteen"
      Case 16: Result = "Sixteen"
      Case 17: Result = "Seventeen"
      Case 18: Result = "Eighteen"
      Case 19: Result = "Nineteen"
      Case Else
    End Select
      Else ' If value between 20-99...
        Select Case Val(Left(TensText, 1))
          Case 2: Result = "Twenty "
          Case 3: Result = "Thirty "
          Case 4: Result = "Forty "
          Case 5: Result = "Fifty "
          Case 6: Result = "Sixty "
          Case 7: Result = "Seventy "
          Case 8: Result = "Eighty "
          Case 9: Result = "Ninety "
          Case Else
        End Select
    Result = Result & GetDigit _
    (Right(TensText, 1)) ' Retrieve ones place.
  End If
  GetTens = Result
End Function








'*******************************************
' Converts a number from 1 to 9 into text. *
'*******************************************


Function GetDigit(Digit)
  Select Case Val(Digit)
    Case 1: GetDigit = "One"
    Case 2: GetDigit = "Two"
    Case 3: GetDigit = "Three"
    Case 4: GetDigit = "Four"
    Case 5: GetDigit = "Five"
    Case 6: GetDigit = "Six"
    Case 7: GetDigit = "Seven"
    Case 8: GetDigit = "Eight"
    Case 9: GetDigit = "Nine"
    Case Else: GetDigit = ""
  End Select
End Function
 
Upvote 0
@CalcSux78, this is partly addressing the output. For example, with 10.123456 the output is Ten and Four Hundred Fifty Six Millionths. The .123 part is getting missed.
 
Upvote 0
My apologies... There was an oversight in my SpellNumber Function adaptation.

All of the functions are required, but you would need to make these adjustments to the SpellNumber Function<!--StartFragment--> (or replace old version with new version)<!--EndFragment-->:
Code:
Function SpellNumber(MyNumber, Optional bMoney = False)

  Dim Dollars, Cents, Temp
  Dim DecimalPlace, Count
  Dim iNumber
  Dim sFract$, iCents
  
  ReDim Place(9) As String
  Place(2) = " Thousand "
  Place(3) = " Million "
  Place(4) = " Billion "
  Place(5) = " Trillion "
  
  iNumber = MyNumber
  ' String representation of amount.
  MyNumber = Trim(str(MyNumber))
  
  ' Position of decimal place 0 if none.
  DecimalPlace = InStr(MyNumber, ".")
  ' Convert cents and set MyNumber to dollar amount.
  If DecimalPlace > 0 Then
    iCents = Mid(MyNumber, DecimalPlace + 1)
[COLOR=#ff0000]    sFract = GetFract(iCents)[/COLOR]
    MyNumber = Trim(Left(MyNumber, DecimalPlace - 1))
  End If
  
  Count = 1
  Do While MyNumber <> ""
    Temp = GetHundreds(Right(MyNumber, 3))
    If Temp <> "" Then Dollars = Temp & Place(Count) & Dollars
    If Len(MyNumber) > 3 Then
      MyNumber = Left(MyNumber, Len(MyNumber) - 3)
      Else
      MyNumber = ""
    End If
    Count = Count + 1
  Loop
  
  If bMoney = True Then
[COLOR=#ff0000]    Cents = GetHundreds(Left(iCents, 2))[/COLOR]
    Select Case Dollars
      Case ""
        Dollars = "No Dollars"
      Case "One"
        Dollars = "One Dollar"
      Case Else
       Dollars = Dollars & " Dollars"
    End Select
    
    Select Case Cents
      Case ""
       Cents = " and No Cents"
      Case "One"
        Cents = " and One Cent"
      Case Else
        Cents = " and " & Cents & " Cents"
    End Select
    SpellNumber = Dollars & Cents
    Exit Function
  End If
  
[COLOR=#ff0000]  Count = 1[/COLOR]
[COLOR=#ff0000]  Do While iCents <> ""[/COLOR]
[COLOR=#ff0000]    Temp = GetHundreds(Right(iCents, 3))[/COLOR]
[COLOR=#ff0000]    If Temp <> "" Then Cents = Temp & Place(Count) & Cents[/COLOR]
[COLOR=#ff0000]    If Len(iCents) > 3 Then[/COLOR]
[COLOR=#ff0000]      iCents = Left(iCents, Len(iCents) - 3)[/COLOR]
[COLOR=#ff0000]      Else[/COLOR]
[COLOR=#ff0000]      iCents = ""[/COLOR]
[COLOR=#ff0000]    End If[/COLOR]
[COLOR=#ff0000]    Count = Count + 1[/COLOR]
[COLOR=#ff0000]  Loop[/COLOR]
  
  If iNumber <> CInt(iNumber) Then
    If Len(Dollars) > 0 Then
      SpellNumber = Dollars & " and " & Cents & sFract
    Else
      SpellNumber = Cents & sFract
    End If
    Exit Function
  End If
  SpellNumber = Dollars
End Function
 
Upvote 0
@CalcSux78, thanks for the edits. It works mostly but for Tenths the output is not correct. For 100.02 the output should be One Hundred and Two Tenths; but, what is getting executed is One Hundred and Two Hundredths. Can you help with the same?
 
Upvote 0

Forum statistics

Threads
1,223,911
Messages
6,175,322
Members
452,635
Latest member
laura12345

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top