kristian
kristian

Reputation: 27

Convert list of strings with scientific notation to decimal numbers

I have a list of strings (numbers with scientific notation) and I want to convert it to real numbers; my idea was convert it using a loop:

    for (var i = 0; i < time.Count; i++)
    {
        timeD.Add(decimal.Parse(time[i], System.Globalization.NumberStyles.Any));
    }

but the values of the outcoming numbers are wrong. The values of my list are:

7.6923076920E-10
1.5384615380E-09
3.2051282050E-09
4.8717948720E-09

and the output is

7,6923076920
15,384615380
32,051282050
48,717948720

Any ideas why this is happening and how do I fix it?

Upvotes: 0

Views: 176

Answers (2)

Horus
Horus

Reputation: 21

If you don't care about Globalization you can declare you own NumberFormatInfo

var nfi = new NumberFormatInfo();

So if your input decimal separator is "." or "," you can set it in

nfi.NumberDecimalSeparator

Then you can use

var num = decimal.Parse(time[i], nfi);

CultureInfo.InvariantCulture always assumes that the decimal separator is "."

Upvotes: 1

Klaus G&#252;tter
Klaus G&#252;tter

Reputation: 12007

It seems that you have a CurrentCulture where the '.' is not the decimal separator. So 7.6923076920E-10 is the same as 76923076920E-10. Try specifying CultureInfo.InvariantCulture on parsing:

 decimal.Parse(time[i], System.Globalization.NumberStyles.Any, CultureInfo.InvariantCulture)

Upvotes: 4

Related Questions