0

Well, I have an VB.net code which get the value from some XML fiels, no problem with it, the issue is that sometimes the values have different format, after the dot, and so when I try to convert the value to a number I get a wrong value.

My code to get the field:

qntXml As XmlNodeList = docXml.GetElementsByTagName("qCom")
qnt = Convert.ToDecimal(quantidadeXml(0).InnerText)

I used to divide the "qnt" field by 100, to get the right value, but it's not working anymore, as you can see below

<qCom>380.00</qCom>
<qCom>170.0000</qCom>

I get the "full" number, for example 38000 while I should get 380,00.

If anyone have an idea how to solve it with C# no problem too.

Universal
  • 45
  • 6
  • This has nothing to do with XML. You are trying to parse a decimal number using your default locale, which obviously uses `.` as the thousand separator. Instead of using `Convert.ToDecimal`, use `decimal.Parse` with the InvariantCultureInfo, eg `decimal.Parse(someText,CultureInfo.InvariantCulture)` – Panagiotis Kanavos Apr 10 '17 at 14:02

1 Answers1

0

Convert.ToDecimal uses the CurrentCulture Property

So you could convert to have the . as comma:

qntXml As XmlNodeList = docXml.GetElementsByTagName("qCom")
var clone = (CultureInfo)CultureInfo.CurrentCulture.Clone();
clone.NumberFormat.NumberDecimalSeparator = ".";
qnt = Convert.ToDecimal(quantidadeXml(0).InnerText, clone);
Mederic
  • 1,949
  • 4
  • 19
  • 36