• Numbers to text without cents (2003)

    Author
    Topic
    #446822

    Dear Doctors,

    I have code from Microsoft web, but I need to modify to eliminate words “Cents”-because wouldn’t be needed in Rupiah currency. Could anyone help me out?

    regards

    Indra

    Viewing 2 reply threads
    Author
    Replies
    • #1087503

      The easiest solution is to change the line

      SpellNumberIDR = Rupiah & Cents

      to

      SpellNumberIDR = Rupiah

      There will be some unused lines in the code, but the overhead is small.

    • #1087504

      (Edited by Jezza on 09-Dec-07 12:33. To comment out a part of the code as per Hans’ suggestion in code below.)

      Hi Indra

      Try this

      Option Explicit
      'Main Function
      Function SpellNumberIDR(ByVal MyNumber)
          Dim Rupiah, Cents, Temp
          Dim DecimalPlace, Count
          ReDim Place(9) As String
          Place(2) = " Thousand "
          Place(3) = " Million "
          Place(4) = " Billion "
          Place(5) = " Trillion "
          ' String representation of amount.
          MyNumber = Trim(Str(MyNumber))
          ' Position of decimal place 0 if none.
          DecimalPlace = InStr(MyNumber, ".")
          ' Convert cents and set MyNumber to Rupiah amount.
          If DecimalPlace > 0 Then
              'Cents = GetTens(Left(Mid(MyNumber, DecimalPlace + 1) & _
                        "00", 2))
              MyNumber = Trim(Left(MyNumber, DecimalPlace - 1))
          End If
          Count = 1
          Do While MyNumber  ""
              Temp = GetHundreds(Right(MyNumber, 3))
              If Temp  "" Then Rupiah = Temp & Place(Count) & Rupiah
              If Len(MyNumber) > 3 Then
                  MyNumber = Left(MyNumber, Len(MyNumber) - 3)
              Else
                  MyNumber = ""
              End If
              Count = Count + 1
          Loop
          Select Case Rupiah
              Case ""
                  Rupiah = "No Rupiah"
              Case "One"
                  Rupiah = "One Rupiah"
               Case Else
                  Rupiah = Rupiah & " Rupiah"
          End Select
          'Select Case Cents
           '   Case ""
            '      Cents = " and No Cents"
             ' Case "One"
              '    Cents = " and One Cent"
               '     Case Else
                '  Cents = " and " & Cents & " Cents"
         ' End Select
          SpellNumberIDR = Rupiah '& Cents
      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
      

      I have left the code in but commented it out so you can see where the changes were made sneaky

      • #1087505

        You can also comment out the instruction

        Cents = GetTens(Left(Mid(MyNumber, DecimalPlace + 1) & _
        “00”, 2))

        and remove the declaration of Cents.

        • #1087507

          Nice one, I overlooked that cheers

          whisperI’ll change my post to show that

          • #1087511

            Thanks Dr. Hans, Dr Jerry,
            prescription works, as usual.

            Indra

    • #1087526

      Here is a version that I wrote many years ago that you pass the currency name as the second parameter and the “cents” name as the third parameter:


      Function NumberToText(Num As Variant, Optional vCurName As Variant, Optional vCent As Variant) As Variant
      Dim TMBT As Variant
      Dim sNum As String, sDec As String, sHun As String, IC As Integer
      Dim Result As String, sCurName As String, sCent As String

      If Application.IsNumber(Num) = False Then
      NumberToText = CVErr(xlValue)
      Exit Function
      End If

      If IsMissing(vCurName) Then
      sCurName = ""
      Else
      sCurName = Trim(CStr(vCurName))
      End If
      If IsMissing(vCent) Then
      sCent = ""
      Else
      sCent = Trim(CStr(vCent))
      End If

      TMBT = Array("", "Thousand", "Million", "Billion", "Trillion", "Quadrillion", "Quintillion", "Sextillion")

      If IsMissing(sCent) Or IsNull(sCent) Then
      sNum = Format(Application.Round(Num, 0), "0")
      Else
      sNum = Format(Application.Round(Num, 2), "0.00")
      sDec = Right(sNum, 2)
      sNum = Left(sNum, Len(sNum) - 3)
      If CInt(sDec) 0 Then
      sDec = "and " & Trim(HundredsToText(CVar(sDec)) & " " & sCent)
      Else
      sDec = ""
      End If
      End If

      IC = 0
      While Len(sNum) > 0
      sHun = Right(sNum, 3)
      sNum = Left(sNum, Application.Max(Len(sNum) - 3, 0))
      If CInt(sHun) 0 Then
      Result = Trim(Trim(HundredsToText(CVar(sHun)) & " " & TMBT(IC)) & " " & Result)
      End If
      IC = IC + 1
      Wend
      Result = Trim(Result & " " & sCurName)
      Result = Trim(Result & " " & sDec)

      NumberToText = Result

      End Function

      Function HundredsToText(Num As Integer) As String
      Dim Units As Variant, Teens As Variant, Tens As Variant
      Dim I As Integer, IUnit As Integer, ITen As Integer, IHundred As Integer
      Dim Result As String

      Units = Array("", "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine")
      Teens = Array("Ten", "Eleven", "Twelve", "Thirteen", "Fourteen", "Fifteen", "Sixteen", "Seventeen", "Eighteen", "Nineteen")
      Tens = Array("", "", "Twenty", "Thirty", "Fourty", "Fifty", "Sixty", "Seventy", "Eighty", "Ninety")

      Result = ""
      IUnit = Num Mod 10
      I = Int(Num / 10)
      ITen = I Mod 10
      IHundred = Int(I / 10)
      If IHundred > 0 Then
      Result = Units(IHundred) & " Hundred"
      End If
      If ITen = 1 Then
      Result = Result & " " & Teens(IUnit)
      Else
      If ITen > 1 Then
      Result = Trim(Result & " " & Tens(ITen) & " " & Units(IUnit))
      Else
      Result = Trim(Result & " " & Units(IUnit))
      End If
      End If

      HundredsToText = Result

      End Function

    Viewing 2 reply threads
    Reply To: Numbers to text without cents (2003)

    You can use BBCodes to format your content.
    Your account can't use all available BBCodes, they will be stripped before saving.

    Your information: