将十进制货币转换为文本金额

时间:2013-10-22 11:04:32

标签: vb.net

有人在vb.net中将十进制货币转换为文本金额函数吗?

例如。 $ 110.25 - >产量:一百十美元二十五美分。

1 个答案:

答案 0 :(得分:1)

我之前做过类似的功能,这是让你开始的好方法,你可以适应它:

#Region " Money Abbreviation "

   ' [ Money Abbreviation Function ]
   '
   ' // By Elektro H@cker
   '
   ' Examples :
   '
   ' MsgBox(Money_Abbreviation(1000))           ' Result: 1 K
   ' MsgBox(Money_Abbreviation(1000000))        ' Result: 1 M
   ' MsgBox(Money_Abbreviation(1500000, False)) ' Result: 1,5 M

   Private Function Money_Abbreviation(ByVal Quantity As Object, _
                                       Optional ByVal Rounded As Boolean = True) As String

       Dim Abbreviation As String = String.Empty

       Select Case Quantity.GetType()

           Case GetType(Int16), GetType(Int32), GetType(Int64)
               Quantity = FormatNumber(Quantity, TriState.False)

           Case Else
               Quantity = FormatNumber(Quantity, , TriState.False)

       End Select

       Select Case Quantity.ToString.Count(Function(character As Char) character = Convert.ToChar("."))

           Case 0 : Return String.Format("${0}", Quantity)
           Case 1 : Abbreviation = "k"
           Case 2 : Abbreviation = "M"
           Case 3 : Abbreviation = "B"
           Case 4 : Abbreviation = "Tr."
           Case 5 : Abbreviation = "Quad."
           Case 6 : Abbreviation = "Quint."
           Case 7 : Abbreviation = "Sext."
           Case 8 : Abbreviation = "Sept."
           Case Else
               Return String.Format("${0}", Quantity)

       End Select

       Return IIf(Rounded, _
              String.Format("{0} {1}", StrReverse(StrReverse(Quantity).Substring(StrReverse(Quantity).LastIndexOf(".") + 1)), Abbreviation), _
              String.Format("{0} {1}", StrReverse(StrReverse(Quantity).Substring(StrReverse(Quantity).LastIndexOf(".") - 1)), Abbreviation))

   End Function

#End Region