How to Convert Currency or Negative Numbers into Words?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • yosiro
    New Member
    • Aug 2012
    • 34

    How to Convert Currency or Negative Numbers into Words?

    I have a module to convert numbers (in positive value) into My Language (indonesian) words below:

    Code:
    Option Compare Database
    
    Public Function ubah_terbilang(xbil As Double)
       Dim nilai, i, j, k, hasil$, HasilAkhir$, Bilangan#, Digit, Rp$, Bil$
     
       If IsNull(xbil) Then
          ubah_terbilang = Null
          Exit Function
       End If
     
    'pengelompokan
        Dim Kel$(1 To 6), Angka$(1 To 9), Sat$(1 To 3)
        Kel$(1) = "Biliun "
        Kel$(2) = "Triliun "
        Kel$(3) = "Miliar "
        Kel$(4) = "Juta "
        Kel$(5) = "Ribu "
        Kel$(6) = ""
     
    'data angka
        Angka$(1) = "Satu "
        Angka$(2) = "Dua "
        Angka$(3) = "Tiga "
        Angka$(4) = "Empat "
        Angka$(5) = "Lima "
        Angka$(6) = "Enam "
        Angka$(7) = "Tujuh "
        Angka$(8) = "Delapan "
        Angka$(9) = "Sembilan "
     
    'satuan
        Sat$(1) = "Ratus "
        Sat$(2) = "Puluh "
        Sat$(3) = ""
     
    'mulai
       Bilangan# = Val(xbil)
       HasilAkhir$ = ""
       GoSub HitungHuruf
       If hasil$ <> "" Then
        HasilAkhir$ = hasil$ + "Rupiah"
       End If
     
    'hitung pecahan
       Bilangan# = Fix((Bilangan# - Fix(Bilangan#) + 0.005) * 100#)
       If Bilangan# > 0 Then
          GoSub HitungHuruf
          If hasil$ <> "" Then
            HasilAkhir$ = HasilAkhir$ + " " + hasil$ + "Sen"
          End If
       End If
     
    ubah_terbilang = HasilAkhir$
    Exit Function
     
    HitungHuruf:
        Rp$ = Right$(String$(18, "0") + LTrim$(Str$(Fix(Bilangan#))), 18)
        hasil$ = ""
     
        If Val(Rp$) = 0 Then Return
     
    'blg bulat
       For i = 1 To 6
          Bil$ = Mid$(Rp$, i * 3 - 2, 3)
     
          If Val(Bil$) = 1 And i = 5 Then
             hasil$ = hasil$ + "Seribu "
     
          ElseIf Val(Bil$) <> 0 Then
             For j = 1 To 3
                Digit = Val(Mid$(Bil$, j, 1))
                If j = 2 And Right$(Bil$, 2) = "10" Then
                   hasil$ = hasil$ + "Sepuluh "
                   Exit For
     
                ElseIf j = 2 And Right$(Bil$, 2) = "11" Then
                   hasil$ = hasil$ + "Sebelas "
                   Exit For
     
                ElseIf j = 2 And Mid$(Bil$, 2, 1) = "1" Then
                   hasil$ = hasil$ + Angka$(Val(Right$(Bil$, 1))) + "Belas "
                   Exit For
     
                ElseIf Digit = 1 And j = 1 Then
                   hasil$ = hasil$ + "Seratus "
     
                ElseIf Digit <> 0 Then
                   hasil$ = hasil$ + Angka$(Digit) + Sat$(j)
     
                End If
             Next
             hasil$ = hasil$ + Kel$(i)
          End If
       Next
       Return
    End Function
    When i type negative numbers, this module not works, example i type -100 the result is empty.

    Please help me to modify this module when i type positive or negative value into field, this module can translate it to other field.

    Thanks
  • jforbes
    Recognized Expert Top Contributor
    • Aug 2014
    • 1107

    #2
    This should/might be close. You'll have to update line 55 to add whatever is needed to the front or end to signify a negative number:
    Code:
    Option Compare Database
    Option Explicit
    
     
     Public Function ubah_terbilang(xbil As Double)
        Dim nilai, i, j, k, hasil$, HasilAkhir$, Bilangan#, Digit, Rp$, Bil$
     
        If IsNull(xbil) Then
           ubah_terbilang = Null
           Exit Function
        End If
     
     'pengelompokan
         Dim Kel$(1 To 6), Angka$(1 To 9), Sat$(1 To 3)
         Kel$(1) = "Biliun "
         Kel$(2) = "Triliun "
         Kel$(3) = "Miliar "
         Kel$(4) = "Juta "
         Kel$(5) = "Ribu "
         Kel$(6) = ""
     
     'data angka
         Angka$(1) = "Satu "
         Angka$(2) = "Dua "
         Angka$(3) = "Tiga "
         Angka$(4) = "Empat "
         Angka$(5) = "Lima "
         Angka$(6) = "Enam "
         Angka$(7) = "Tujuh "
         Angka$(8) = "Delapan "
         Angka$(9) = "Sembilan "
     
     'satuan
         Sat$(1) = "Ratus "
         Sat$(2) = "Puluh "
         Sat$(3) = ""
     
     'mulai
        Bilangan# = Val(xbil)
        HasilAkhir$ = ""
        GoSub HitungHuruf
        If hasil$ <> "" Then
         HasilAkhir$ = hasil$ + "Rupiah"
        End If
     
     'hitung pecahan
        Bilangan# = Fix((Bilangan# - Fix(Bilangan#) + 0.005) * 100#)
        If Bilangan# > 0 Then
           GoSub HitungHuruf
           If hasil$ <> "" Then
             HasilAkhir$ = HasilAkhir$ + " " + hasil$ + "Sen"
           End If
        End If
     
    [iCODE] If xbil < 0 Then HasilAkhir$ = "Negative " & HasilAkhir$[/iCODE]
     ubah_terbilang = HasilAkhir$
     Exit Function
     
    HitungHuruf:
         [iCODE]Rp$ = Right$(String$(18, "0") + LTrim$(Str$(Fix(Abs(Bilangan#)))), 18)[/iCODE]
         hasil$ = ""
     
         If Val(Rp$) = 0 Then Return
     
     'blg bulat
        For i = 1 To 6
           Bil$ = Mid$(Rp$, i * 3 - 2, 3)
     
           If Val(Bil$) = 1 And i = 5 Then
              hasil$ = hasil$ + "Seribu "
     
           ElseIf Val(Bil$) <> 0 Then
              For j = 1 To 3
                 Digit = Val(Mid$(Bil$, j, 1))
                 If j = 2 And Right$(Bil$, 2) = "10" Then
                    hasil$ = hasil$ + "Sepuluh "
                    Exit For
     
                 ElseIf j = 2 And Right$(Bil$, 2) = "11" Then
                    hasil$ = hasil$ + "Sebelas "
                    Exit For
     
                 ElseIf j = 2 And Mid$(Bil$, 2, 1) = "1" Then
                    hasil$ = hasil$ + Angka$(Val(Right$(Bil$, 1))) + "Belas "
                    Exit For
     
                 ElseIf Digit = 1 And j = 1 Then
                    hasil$ = hasil$ + "Seratus "
     
                 ElseIf Digit <> 0 Then
                    hasil$ = hasil$ + Angka$(Digit) + Sat$(j)
     
                 End If
              Next
              hasil$ = hasil$ + Kel$(i)
           End If
        Next
        Return
     End Function

    Comment

    • yosiro
      New Member
      • Aug 2012
      • 34

      #3
      Thanks Jforbes, its work, but the "negative" word is now show in the field, like picture attached, can it be removed?

      Comment

      • jforbes
        Recognized Expert Top Contributor
        • Aug 2014
        • 1107

        #4
        Sure, I put that in there thinking that you are trying to include negative numbers in your display. Just remove line 55 from post #2.

        So if you remove the negative notation, what do you expect the function to do when it is passed a negative number? ...How are negative number expressed in your language?

        Comment

        • yosiro
          New Member
          • Aug 2012
          • 34

          #5
          It's done now. I'm making a receipt from this database. Negative number for debit transaction so I can count each group report with sum function not (group a - group b) because there is no standard function for that. Thanks jforbes

          Comment

          Working...