字符串转换为十进制解析C# [英] String to decimal parsing C#

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

问题描述

我试图解析或转换成字符串在C#十进制。

I am trying to parse or convert a string to decimal in C#.

我需要能够解析一样,

$ 123,345,676.8999为它的等效123345676.90。

$123,345,676.8999 to its equivalent 123345676.90.

我需要的小数点后只有2个名额,并需要适当地舍入。

I need to have only 2 places after the decimal point and that needs to be suitably rounded.

灿你们,请提出一个办法做到上面?基本上,以货币形式的字符串(以$,英镑符号等)。我应该能够解析它,并将其转换为十进制

Can you guys please suggest a way to do the above? Basically, any string in the form of currency (with $,pound symbol etc). I should be able to parse it and convert it to decimal.

推荐答案

试试这个:

var val = double.Parse("$123,345,676.8999", NumberStyles.AllowThousands | NumberStyles.AllowDecimalPoint | NumberStyles.AllowCurrencySymbol);
val = Math.Round(val, 2);

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

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