[Solved] Parsing all the doubles from a string C# [closed]


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.)

Then you could do this:

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();

That gives you:

Array of Decimals

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

9

solved Parsing all the doubles from a string C# [closed]