Having difficulties converting a string into decimal
Thanks for taking the time to assist me with my problem.
In the code I'm writing, I'm iterating through a table, I get the appropriate values (confirmed it using the debugger) and I'm parsing them to the appropriate types before and finally I add them to an Object to be serialized into XML.
However, I bumped into a problem and that is I can't seem to find a way to parse the string into a decimal value. Take a look:
if (DateTime.TryParse(dateString, culture, styles, out date))
{
decimal LastValue;
string vrednost = String.Format("{0:0,0.0}",
row.SelectSingleNode("td[2]").InnerText);
if (Decimal.TryParse(vrednost, out LastValue))
开发者_如何学Python list.Add(new StockEntry
{
Date = date,
PoslednaCena = LastValue
...
}
Note that the value of vrednost is 4.451,00 and I suspect that if I convert it to 4,451.00 it will get parsed.
I've succeeded in parsing date into the appropriate datetime value. However, the value of LastValue is always 0. I've exhausted all the resources that I know of. Do you have any idea how to solve my problem?
Thank you in advance!
This formatting will do nothing because you can't format strings like this. You have to use parse method with additional parameters and specify your own format
string s2 = "4.451,00";
NumberFormatInfo numberFormatInfo = new NumberFormatInfo();
numberFormatInfo.NumberDecimalSeparator = ",";
numberFormatInfo.NumberGroupSeparator = ".";
var d = decimal.Parse(s2, numberFormatInfo);
I think that your problem might be due to the culture used for the parsing. Try using CultureInfo.InvariantCulture for your parsing. It should work with "," as thousands separator and "." as decimal separator.
Decimal.TryParse(vrednost, NumberStyles.Number, CultureInfo.InvariantCulture, out LastValue);
If you want to swap them you could use another culture. Italian, for instance, works with your format (not sure about the others), so your code for "4.451,00" would look like:
Decimal.TryParse(vrednost, NumberStyles.Number, CultureInfo.GetCultureInfo("it"), out LastValue);
If you want to use a custom culture instead of forcing some culture which does what you want you can simply create your NumberFormatInfo class and pass it to the parse method.
NumberFormatInfo decimalNumber = new NumberFormatInfo();
decimalNumber.NumberDecimalSeparator = ",";
decimalNumber.NumberGroupSeparator = ".";
Decimal.TryParse(vrednost, NumberStyles.Number, decimalNumber, out LastValue);
Your row.SelectSingleNode("td[2]").InnerText is a string and you are trying to format it like a decimal.
Try parsing it directly:
decimal LastValue;
string vrednost = row.SelectSingleNode("td[2]").InnerText;
if (Decimal.TryParse(vrednost, out LastValue))
Check your cultureinfo at first and set it appropriately.
CultureInfo MyUsersCulture = Thread.CurrentThread.CurrentCulture;
Console.WriteLine("The culture: "+ MyUsersCulture.Name);
Thread.CurrentThread.CurrentCulture = new CultureInfo("de-DE");
ConsoleWriteLine("The culture: " + Thread.CurrentThread.CurrentCulture);
精彩评论