Oddness converting from string to double

I am parsing an XML node, where all of the values are obviously stored as strings.

The value of the attribute xloc in the XML file is "0.02"

r.xloc = double.FromString(item.GetAttribute("xloc"))

OR

r.xloc = item.GetAttribute("xloc").ToDouble

Both result in a value in r.xloc of “0.

However, if I break it down more explicitly, like this:

var temp as string = item.GetAttribute("xloc")
var tempDouble as double
tempDouble = double.FromString(temp)
r.xLoc = tempDouble

temp = "0.02"
tempDouble = 0.02
r.xloc = 0.02

Which is what I want. But why is it that neither item.GetAttribute("xloc").ToDouble or double.FromString(item.GetAttribute("xloc")) seem to be directly assignable to r.xloc? Shouldn’t both of those return a double from the string provided?

That is my guess. Can you create a sample project that recreates this problem?
What version of Xojo are you using?
What platform are you using?

Mac. 2024 r4.2

I have an older version of Xojo on my laptop at home and might try to update it to the same version as I have at work later tonight so I can make a project, but it’s fairly straightforward, what I’m doing. If I can’t do it tonight, I’ll try to do it tomorrow AM when I’m back at work.

I smell Xojo bug

Make sure you set the locale to Nil when you do that conversion. ToString considers that and if it doesn’t match your locale, you’ll get odd results.

I cannot reproduce your results. Both ways (one-liner or intermediary string variable) yield the double value 0.02.

Edit: Xojo 2025r2.1, Windows 10
Edit 2: Tried both double.FromString() and ToDouble

I can break things in systems whose locales don’t use period as decimal, so using Val fix it as:

// Sample XML -- Xojo 2025r2.1
Var xml_str As String = "<?xml version=""1.0"" encoding=""UTF-8""?>"+_
"<anyitem id=""123"" xloc=""0.02""></anyitem>"

Var refval As Double = 0.02

// Load
Var xmlDoc As New XMLDocument(xml_str)

// Get the root element (the 'anyitem' node in this case)
Var item As XMLNode = xmlDoc.DocumentElement

// Get the value of the 'xloc' attribute
Var xloc_str As String = item.GetAttribute("xloc")

Var xloc_dbl As Double
xloc_dbl = xloc_str.Val
//xloc_dbl = xloc_str.ToDouble  //--- This return 2 in a locale where comma is the decimal marker

If xloc_dbl<>refval Then 
  MessageBox "Invalid value : "+xloc_dbl.ToString
Else
  MessageBox "OK! It Works"
End

Quit

1 Like

The documentation is clear about this:
String.ToDouble → Convert using user locale
String.Val → Convert using Standard (English) locale notation (only for decimal separator)

So I spent some more time on this and I think it’s related to ImportNode. If I directly pull a node from the XML file like this:

var item as XMLNode = XMLFile.DocumentElement.Child(0)

Then both

item.GetAttribute("xloc").ToDouble

and

double.FromString(item.GetAttribute("xloc"))

works fine.

If i use ImportNode to extract the node from the master XML file, the node is correctly imported (I can see it and all of its attributes in the debugger and when I convert it to a string for display) but the attributes are empty when I try to access them in code. So it seems to be related to issue #79744 which I reported the other day, after we discussed it here: How to insert an XML node into an existing XMLDocument