I'm Trying to handle extra space in result in textbox in vb.net
Is there something wrong with my code?
or there is another solution?
Please Guide me
Thanks
Below is the code I used :
Public Class Form6
Public Function Terbilang(ByVal nilai As Integer) As String
Dim bilangan As String() = {"", "Satu", "Dua", "Tiga", "Empat", "Lima", "Enam", "Tujuh", "Delapan", "Sembilan", "Sepuluh", "Sebelas"}
If nilai < 12 Then
Return " " & System.Text.RegularExpressions.Regex.Replace(bilangan(CInt(nilai)), "^\s+|\s+$", " ")
ElseIf nilai < 20 Then
Return System.Text.RegularExpressions.Regex.Replace(Terbilang(nilai - 10) & " Belas", "^\s+|\s+$", " ")
ElseIf nilai < 100 Then
Return System.Text.RegularExpressions.Regex.Replace((Terbilang(CInt((nilai \ 10))) & " Puluh") + Terbilang(nilai Mod 10), "^\s+|\s+$", " ")
ElseIf nilai < 200 Then
Return System.Text.RegularExpressions.Regex.Replace(" Seratus" & Terbilang(nilai - 100), "^\s+|\s+$", " ")
ElseIf nilai < 1000 Then
Return System.Text.RegularExpressions.Regex.Replace((Terbilang(CInt((nilai \ 100))) & " Ratus") + Terbilang(nilai Mod 100), "^\s+|\s+$", " ")
ElseIf nilai < 2000 Then
Return System.Text.RegularExpressions.Regex.Replace(" Seribu" & Terbilang(nilai - 1000), "^\s+|\s+$", " ")
ElseIf nilai < 1000000 Then
Return System.Text.RegularExpressions.Regex.Replace((Terbilang(CInt((nilai \ 1000))) & " Ribu") + Terbilang(nilai Mod 1000), "^\s+|\s+$", " ")
ElseIf nilai < 1000000000 Then
Return System.Text.RegularExpressions.Regex.Replace((Terbilang(CInt((nilai \ 1000000))) & " Juta") + Terbilang(nilai Mod 1000000), "^\s+|\s+$", " ")
ElseIf nilai < 1000000000000 Then
Return System.Text.RegularExpressions.Regex.Replace((Terbilang(CInt((nilai \ 1000000000))) & " Milyar") + Terbilang(nilai Mod 1000000000), "^\s+|\s+$", " ")
ElseIf nilai < 1000000000000000 Then
Return System.Text.RegularExpressions.Regex.Replace((Terbilang(CInt((nilai \ 1000000000000))) & " Trilyun") + Terbilang(CInt(nilai Mod 1000000000000)), "^\s+|\s+$", " ")
Else
Return ""
End If
End Function
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
TextBox2.Text = Terbilang(CInt(TextBox1.Text))
End Sub
End Class
Below are examples of problems with extra space :
Example 1 | |
---|---|
Input | 350000 |
Output | Tiga Ratus Lima Puluh Ribu Rupiah |
Expected | Tiga Ratus Lima Puluh Ribu Rupiah |
Example 2 | |
---|---|
Input | 20000 |
Output | Dua Puluh Ribu Rupiah |
Expected | Dua Puluh Ribu Rupiah |
Example 3 | |
---|---|
Input | 100 |
Output | Seratus Rupiah |
Expected | Seratus Rupiah |
After updated code from @AndrewMorton
Example 1 | |
---|---|
Input | 350000 |
Output | Tiga Ratus Lima Puluh Ribu Rupiah |
Expected | Tiga Ratus Lima Puluh Ribu Rupiah |
Example 2 | |
---|---|
Input | 20000 |
Output | Dua Puluh Ribu Rupiah |
Expected | Dua Puluh Ribu Rupiah |
Only example 3 solved
Example 3 | |
---|---|
Input | 100 |
Output | Seratus Rupiah |
Expected | Seratus Rupiah |
Update code recommendation from @WiktorStribiżew
Public Function Terbilang(ByVal nilai As Integer) As String
Dim bilangan As String() = {"", "Satu", "Dua", "Tiga", "Empat", "Lima", "Enam", "Tujuh", "Delapan", "Sembilan", "Sepuluh", "Sebelas"}
If nilai < 12 Then
Return Trim(bilangan(CInt(nilai)))
ElseIf nilai < 20 Then
Return Trim(Terbilang(nilai - 10) & " Belas")
ElseIf nilai < 100 Then
Return Trim(Terbilang(CInt((nilai \ 10))) & " Puluh" + Terbilang(nilai Mod 10))
ElseIf nilai < 200 Then
Return Trim(" Seratus" & Terbilang(nilai - 100))
ElseIf nilai < 1000 Then
Return Trim(Terbilang(CInt((nilai \ 100))) & " Ratus" + Terbilang(nilai Mod 100))
ElseIf nilai < 2000 Then
Return Trim(" Seribu" & Terbilang(nilai - 1000))
ElseIf nilai < 1000000 Then
Return Trim(Terbilang(CInt((nilai \ 1000))) & " Ribu" + Terbilang(nilai Mod 1000))
ElseIf nilai < 1000000000 Then
Return Trim(Terbilang(CInt((nilai \ 1000000))) & " Juta" + Terbilang(nilai Mod 1000000))
ElseIf nilai < 1000000000000 Then
Return Trim(Terbilang(CInt((nilai \ 1000000000))) & " Milyar" + Terbilang(nilai Mod 1000000000))
ElseIf nilai < 1000000000000000 Then
Return Trim(Terbilang(CInt((nilai \ 1000000000000))) & " Trilyun" + Terbilang(CInt(nilai Mod 1000000000000)))
Else
Return ""
End If
End Function
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
TextBox2.Text = Trim(Terbilang(CInt(TextBox1.Text)) + " Rupiah")
End Sub
so only example 1 is unsolved and Others Solved
Example 1 | |
---|---|
Input | 350000 |
Output | Tiga RatusLima Puluh Ribu Rupiah |
Expected | Tiga Ratus Lima Puluh Ribu Rupiah |
Getting the white spaces right is not that trivial. Therefore, I suggest building the result in a List(Of String)
and then join the result with a white space as separator.
I split the task in a main function initializing the list, calling a Sub doing the calculation and joining the result:
Public Function Terbilang(ByVal nilai As Long) As String
Dim result = New List(Of String)
TerbilangRecursive(result, nilai)
Return String.Join(" ", result)
End Function
The calculation happens in a recursive Sub. It is passed the result list where it can add elements and the value as parameter.
Private Sub TerbilangRecursive(result As List(Of String), nilai As Long)
Static bilangan As String() = {"", "Satu", "Dua", "Tiga", "Empat", "Lima",
"Enam", "Tujuh", "Delapan", "Sembilan", "Sepuluh", "Sebelas"}
If nilai < 12 Then
If nilai > 0 Then ' Do not add an empty entry
result.Add(bilangan(nilai))
End If
ElseIf nilai < 20 Then
TerbilangRecursive(result, nilai - 10)
result.Add("Belas")
ElseIf nilai < 100 Then
TerbilangRecursive(result, nilai \ 10)
result.Add("Puluh")
TerbilangRecursive(result, nilai Mod 10)
ElseIf nilai < 200 Then
result.Add("Seratus")
TerbilangRecursive(result, nilai - 100)
ElseIf nilai < 1_000 Then
TerbilangRecursive(result, nilai \ 100)
result.Add("Ratus")
TerbilangRecursive(result, nilai Mod 100)
ElseIf nilai < 2_000 Then
result.Add("Seribu")
TerbilangRecursive(result, nilai - 1_000)
ElseIf nilai < 1_000_000 Then
TerbilangRecursive(result, nilai \ 1_000)
result.Add("Ribu")
TerbilangRecursive(result, nilai Mod 1_000)
ElseIf nilai < 1_000_000_000 Then
TerbilangRecursive(result, nilai \ 1_000_000)
result.Add("Juta")
TerbilangRecursive(result, nilai Mod 1_000_000)
ElseIf nilai < 1_000_000_000_000 Then
TerbilangRecursive(result, nilai \ 1_000_000_000)
result.Add("Milyar")
TerbilangRecursive(result, nilai Mod 1_000_000_000)
ElseIf nilai < 1_000_000_000_000_000 Then
TerbilangRecursive(result, nilai \ 1_000_000_000_000)
result.Add("Trilyun")
TerbilangRecursive(result, nilai Mod 1_000_000_000_000)
Else
' No action
End If
End Sub
A few things to note:
bilangan
array with the Indonesian number names Static
. This means that this array will be created and initialized only once instead of at each call of the Sub.Integer
which is roughly 2 billions. Therefore I am using the 64 bit type Long
having a maximum of about 9.22 * 10^18 instead of the 32 bit Integer
type.\
already returns a truncated integer value. Therefore, there is no need to use CInt(...)
or CLng(...)
.""
for the zero digit._
as a digit separator to enhance readability. I used it as thousands separator.As test setup I used this routine in a Console application:
Sub Test()
Dim examples As (value As Long, expected As String)() = {
(350_000, "Tiga Ratus Lima Puluh Ribu Rupiah"),
(20_000, "Dua Puluh Ribu Rupiah"),
(100, "Seratus Rupiah")}
For Each example In examples
Console.WriteLine($"Value = {example.value}")
Console.WriteLine($"Expected = {example.expected}")
Console.WriteLine($"> Result = {Terbilang(example.value)} Rupiah")
Console.WriteLine()
Next
Console.ReadKey()
End Sub
It creates an example array of Tuples with an input value and the expected result and then executes every example in a loop. I am also using Interpolated Strings. It produces the following output:
Value = 350000
Expected = Tiga Ratus Lima Puluh Ribu Rupiah
> Result = Tiga Ratus Lima Puluh Ribu Rupiah
Value = 20000
Expected = Dua Puluh Ribu Rupiah
> Result = Dua Puluh Ribu Rupiah
Value = 100
Expected = Seratus Rupiah
> Result = Seratus Rupiah