如何使用 VB.NET 创建可以将实数格式化为货币字符串的函数?



我需要创建一个函数,该函数可以将实数格式化为以下格式的货币:

  1. 它应该包括一个前导货币字符,如$
  2. 数千个逗号分隔符
  3. 舍入到定义的小数位数
  4. 使用前导和尾随零填充

固定

Imports System
Imports System.Collections
Public Module Module1
Public Sub Main()
Console.WriteLine(Convert(12345678))
End Sub
Function Convert(number) As String
Dim str as String = ""
While number > 0
str = (number MOD 1000).ToString() + str
number = number  1000
If number > 0
str = "," + str
End If
End While
Return "$" + str
End Function
End Module

https://dotnetfiddle.net/EYJvMC

Imports System.IO
Public Module Module1
Dim value, result As Decimal
Public Sub main()
result = convert()
Console.ReadKey()
End Sub
Function convert()
Console.Write("Enter value: ")
value = Console.ReadLine()
Console.Clear()
Console.WriteLine("$" & value.ToString("#,###,###.00"))
Return value
End Function
End Module

最新更新