在 VB.NET 中将字符串转换为十进制 [英] Convert a string to decimal in 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.
这篇关于在 VB.NET 中将字符串转换为十进制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文