I have a XML node with attributes like this:
<pad name="E" x="2.5" y="7" drill="1.3"/>
When I assign the Attributes["x"]
into a double I get the result 25, not 2.5 but without any complaints or errors.
To get a correct conversion I first have to assign the attribute to a string, replace the decimal '.' to a decimal ',' and then convert the string to a double. It is clearly that the Attribute["x"]
can't convert but it doesn't say anything! (bug?!?)
Here is the code with faulty conversion:
double x = XMLNode->Attributes["x"];
This gives a faulty x of 25 instead of 2.5 and here is my work around:
String sd = XMLNode->Attributes["x"];
if (sd.Pos(".")) sd[sd.Pos(".")] = ',';
double x = sd.ToDouble();
This gives the correct value in x (2.5)
There MUST be a more simple way to do this!
// Thanks
XML attributes are arbitrary string values if you are not using an XSD to coherce the data, such as with the IDE's XML Data Binding wizard. The
Attributes[]
property returns anOleVariant
, which in this case is going to contain aSystem::String
in it. When aSystem::String
is converted to adouble
using theOleVariant
'sdouble
conversion operator or theString::ToDouble()
method, the conversion uses the globalSysUtils::DecimalSeparator
variable, which is initialized using your PC's locale settings, which are clearly using the,
character as a decimal separator instead of the.
character. XML has no way of knowing that locale setting.Since you are using a modern version of C++Builder, you can use the overloaded version of the
StrToFloat()
function that lets you pass in aTFormatSettings
record as input. You can then specific.
as theTFormatSettings::DecimalSeparator
to use for the conversion, eg: