Now i have this problem here with comma and point in decimal point and thousands separator.
My program gets prices from different sources .
some american some european Some prices comes like this 2000,0.20 for 20000.20
and some like 2000.0,20 for again 20000.20
I couldn't find a way to make my code to recognize these two formats . i tried to use replace to turn the comma to dot but if there is thousands separator in the number i get problems. how can i convert string to double without these kind of problems ?
I tried this but its just dont work if there is two different culture
double.TryParse(price, NumberStyles.Currency, CultureInfo.InvariantCulture, out priceD);
This is what I came up with
double FixUnknownCurrency(string amountText)
{
amountText = amountText?.Trim()?.Replace(" ", string.Empty);
if(string.IsNullOrWhiteSpace(amountText))
return 0d;
if(amountText.Length < 3)
return double.Parse(amountText);
var currencyDecimal = amountText[amountText.Length-3];
if(Char.IsNumber(currencyDecimal))
return double.Parse(amountText);
if(currencyDecimal == '.' || currencyDecimal == ',')
{
amountText = amountText.Replace(",", string.Empty).Replace(".", string.Empty);
return double.Parse(amountText) / 100d;
}
return double.Parse(amountText);
}
Try it out here. Running late for train: https://dotnetfiddle.net/HTiL4s