想要将金额转换为印度尼西亚语中的单词

Want to Convert Amount to words in Indonesian Language

提问人:Sohil Bagwan 提问时间:11/5/2023 最后编辑:SSSSohil Bagwan 更新时间:11/7/2023 访问量:41

问:

请帮助我修复旨在将值转换为其印度尼西亚语描述的代码。Double

我的代码没有给出正确的翻译,例如 12.2 没有正确翻译。

Imports System

Public Function ConvertDecimalToWords(decimalNumber As Double) As String
    Dim result As String = ""

    ' Define arrays for words in Indonesian
    Dim units() As String = {"", "satu", "dua", "tiga", "empat", "lima", "enam", "tujuh", "delapan", "sembilan"}
    Dim tens() As String = {"", "sepuluh", "dua puluh", "tiga puluh", "empat puluh", "lima puluh", "enam puluh", "tujuh puluh", "delapan puluh", "sembilan puluh"}

    ' Split the decimal number into its integer and fractional parts
    Dim integerPart As Integer = CInt(Math.Floor(decimalNumber))
    Dim fractionalPart As Integer = CInt(Math.Round((decimalNumber - integerPart) * 100))

    ' Convert the integer part to words
    If integerPart > 0 Then
        result = ConvertToWords(integerPart) & " koma "
    End If

    ' Convert the fractional part to words
    If fractionalPart > 0 Then
        result &= ConvertToWords(fractionalPart)
    Else
        result &= "nol"
    End If

    ' Function to convert a number to words
    Function ConvertToWords(number As Integer) As String
        If number < 10 Then
            Return units(number)
        ElseIf number < 20 Then
            Return tens(number - 10)
        Else
            Dim digit As Integer = number \ 10
            Dim remainder As Integer = number Mod 10
            Return tens(digit) & " " & units(remainder)
        End If
    End Function

    Return result
End Function
vb.net

评论

1赞 jmcilhinney 11/5/2023
这里有很多问题要解决。首先,请查看您的问题的预览,如果格式混乱,请不要提交。如果您不知道如何正确设置格式,请使用帮助中心进行学习。它只是一个像其他编辑器一样的文本编辑器,所以我希望你知道如何使用它。其次,不要在你的问题中添加一堆不相关的标签来诱骗人们阅读它。使用仅直接应用于问题的标签。这里没有任何与 UiPath、Python 甚至 C# 相关的内容。
1赞 jmcilhinney 11/5/2023
最后,SO 不是一个你说“我想做 X,给我代码”的地方。我们在这里帮助解决具体问题,而不仅仅是为您编写代码。您已经提供了现有的代码,但您没有给出任何说明它有什么问题。您需要准确解释代码的行为方式和位置是否符合您的预期。如果你不知道,那么你需要先解决它,然后再发布。调试代码并逐行执行,检查每个步骤的状态。如果状态与您的期望不符,这就是问题所在。
0赞 SSS 11/6/2023
请不要关闭这个问题,这是一个非常有趣的问题。Asker 无法将数字的小数部分转换为单词
0赞 Craig 11/6/2023
@SSS成为一个有趣的问题是必要的,但不足以成为一个好的 Stack Overflow 问题。需要明确问题是什么,以便问题和答案对不仅仅是提出问题的人有用。
0赞 SSS 11/7/2023
@Craig,我觉得 Asker 提供了足够的信息......他正试图将数字变成印尼语的等价物。

答:

-1赞 SSS 11/6/2023 #1

您需要更改一些事项。

  1. 在模块中将数组设为私有
  2. 将 ConvertToWords() 函数移到另一个函数之外
  3. 使用 String 操作提取数字的小数部分

(不确定 #3 是否必需,我真的不会说印尼语)

  ' Define arrays for words in Indonesian
  Private units() As String = {"", "satu", "dua", "tiga", "empat", "lima", "enam", "tujuh", "delapan", "sembilan"}
  Private tens() As String = {"", "sepuluh", "dua puluh", "tiga puluh", "empat puluh", "lima puluh", "enam puluh", "tujuh puluh", "delapan puluh", "sembilan puluh"}

  Public Function ConvertDecimalToWords(decimalNumber As Double) As String
    Dim result As String = ""

    ' Split the decimal number into its integer and fractional parts
    Dim integerPart As Integer = Math.Truncate(decimalNumber)
    Dim fractionalPart As Integer = 0
    Dim decimalString = decimalNumber.ToString
    Dim dotPos = decimalString.IndexOf(".")
    If dotPos > -1 Then
      fractionalPart = Val(decimalString.Substring(dotPos + 1))
    End If

    ' Convert the integer part to words
    If integerPart > 0 Then
      result = ConvertToWords(integerPart) & " koma "
    End If

    ' Convert the fractional part to words
    If fractionalPart > 0 Then
      result &= ConvertToWords(fractionalPart)
    Else
      result &= "nol"
    End If
    Return result
  End Function

  ' Function to convert a number to words
  Function ConvertToWords(number As Integer) As String
    If number < 10 Then
      Return units(number)
    ElseIf number < 20 Then
      Return tens(number - 10)
    Else
      Dim digit As Integer = number \ 10
      Dim remainder As Integer = number Mod 10
      Return tens(digit) & " " & units(remainder)
    End If
  End Function