在VB.NET中将字符串转换为十进制

在VB.NET中将字符串转换为十进制

问题描述:

将字符串转换为十进制的最简单方法是什么?

What will be the easiest way to convert a string to decimal?

输入:

a = 40000.00-

输出将为

40,000.00-

我尝试使用此代码:

Dim a as string

a = "4000.00-"

a = Format$(a, "#,###.##")
console.writeline (a)

使用 Decimal.Parse 转换为十进制数字,然后使用 .ToString("format here")转换回字符串.

Use Decimal.Parse to convert to decimal number, and then use .ToString("format here") to convert back to a string.

Dim aAsDecimal as Decimal = Decimal.Parse(a).ToString("format here")

最后的方法(不推荐):

Last resort approach (not recommended):

string s = (aAsDecimal <0) ? Math.Abs(aAsDecimal).ToString("##,###0.00") + "-" : aAsDecimal .ToString("##,###0.00");

您将不得不转换为Visual Basic.

You will have to translate to Visual Basic.