本文介绍了解析所有的双打从一个字符串C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我实际上有这样的字符串:

I actually have strings formed like this one:

 { Desc = Marketcap, Val = 
    1,270.10 BTC
    706,709.04 USD
    508,040.00 EUR
    4,381,184.55 CNY
    425,238.14 GBP
    627,638.19 CHF
    785,601.09 CAD
    72,442,058.40 JPY
    787,357.97 AUD
    7,732,676.06 ZAR
 }

我需要解析出所有的双打,我实际上是坚持的。我可以这样做吗?

I'd need to parse out all the doubles in it, and I am actually stuck with that. How may I do that?

编辑:我实际上知道我要解析的数字是多少,这是固定的,就像在该字符串中(这只是改变的数字) 。我不需要符号,就像BTC,USD等一样。

I actually know how many numbers I am going to parse, that's fixed, like in that string (that's just the number that changes). I don't need the notation, neither (like BTC, USD, etc)

推荐答案

如果你的数据实际上是这样的:

If your data actually looks like this:

var data = new
{
    Desc = "Marketcap",
    Val = @"1,270.10 BTC
706,709.04 USD
508,040.00 EUR
4,381,184.55 CNY
425,238.14 GBP
627,638.19 CHF
785,601.09 CAD
72,442,058.40 JPY
787,357.97 AUD
7,732,676.06 ZAR",
};

(因为你的问题不清楚。)

(Because what you have in your question is unclear.)

然后你可以这样做:

var query =
    from d in data.Val
        .Split(
            Environment.NewLine.ToCharArray(),
            StringSplitOptions.RemoveEmptyEntries)
    select decimal.Parse(
        d.Split(' ')[0],
        System.Globalization.CultureInfo.GetCultureInfo("en-au"));

decimal[] array = query.ToArray();

这给你:

此外,您要解析为十进制,而不是双重,因为十进制是准确的财务计算和 Double 可能会导致舍入误差。

Also, you want to parse this as Decimal, not Double, as Decimal is accurate for financial calculations and Double can lead to rounding errors.

这篇关于解析所有的双打从一个字符串C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

11-03 07:45