在C#中将字符串转换为十进制 [英] converting string to decimal in c#

查看:119
本文介绍了在C#中将字符串转换为十进制的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在使用decimal.parse将字符串转换为十进制值时遇到一些问题.这是我的代码行:

I am having some problems converting string to decimal values with decimal.parse. This is the line of code I have:

fixPrice = decimal.Parse(mItemParts.Groups["price"].Value.Replace("$", "").Replace(" ", "").Replace("usd", ""));     

我要转换的值是:'$ 779.99'

The value from which I am trying to convert is: '$779.99'

然后,一旦解析为十进制,我得到的值是:77999.

Then once the parsing to decimal happens, I am getting this value: 77999.

我希望获得779.99,而不是77999.在此先感谢Laziale

I would like to get 779.99 instead of 77999. Thanks in advance, Laziale

包含的正则表达式:"@" \ [^ \"] +?)\" [[^〜] +?\] +?src = \"((?[^ \"] +?)\""[^>] +?title = \"(?[^^"] +?)\""[^〜] +?price \">(?[^ \<] +?)\< [^〜] +?\(?[^ \<] +?)\

Regex included: "@"\[^\""]+?)\""[^~]+?\]+?src=\""(?[^\""]+?)\""[^>]+?title=\""(?[^\""]+?)\""[^~]+?price\"">(?[^\<]+?)\<[^~]+?\(?[^\<]+?)\

推荐答案

我会使用 Decimal.TryParse() :

decimal parsedDecimal = 0;
string yourCurrency = "$779.99";
bool didParse = Decimal.TryParse(yourCurrency,
                                 NumberStyles.Currency,
                                 new CultureInfo("en-US"), out parsedDecimal);

if(didParse) {
    // Parse succeeded
}
else {
    // Parse failed
}

这篇关于在C#中将字符串转换为十进制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆