Xojo r 3.1 convertion trouble

MacOS Sonoma

Updating old projects I detected a (bug?) converting a field values to double

X defined as a window double

Set to field pressed:

x = 25.5
TextFieldX.Text = x.ToString

You see in the window :

image

Then

Get from field

Sub Pressed() Handles Pressed
  
  x = TextFieldX.Text.ToDouble
  MessageBox ( "x = " + x.ToString)
End Sub

image

This is terrible

What is your countries convention on decimal places vs thousands separator? Do you use , or . for decimals? Could it be something to do with that?

2 Likes

Taken from: Double — Xojo documentation

ToString(locale As Locale = Nil, Optional format As String) As String

Converts a Double value to a String value using the supplied locale and format.

Sure is this, but I did not changed country or system preferences, and it worked till now.

First, in older Xojo version this worked.

I tried

Var currentLocale As Locale
currentLocale = currentLocale.Current
MessageBox ( "x = " + x.ToString(currentLocale.DecimalSeparator))

But get wrong result again

Have you swapped from:

x =  Val( TextFieldX.Text )
MessageBox ( "x = " + Str( x ) )

Val and Str always use . as a decimal separator. .ToDouble and .ToString take account of your system settings.

If System is set to use , for decimal

String Val() .ToDouble
25.5 25.5 255
25,5 25 25.5

If System is set to use . for decimal

String Val() .ToDouble
25.5 25.5 25.5
25,5 25 255
2 Likes

If you desire to ignore the system locale:

x = 25.5
TextFieldX.Text = x.ToString(Locale.Raw) // always use "." as decimal

No as logical…

try this:

Var x As Double = 22.5

MessageBox x.ToString(Locale.Raw)

TextFieldX.Text = x.ToString(Locale.Raw)

x = TextFieldX.Text.ToDouble
MessageBox x.ToString(Locale.Raw)

First message show: 22.5
Second one: 225

Tested with macOS Ventura 13.7, results 22.5 and 22.5 with US region (OS).

Var currentLocale As Locale
currentLocale = currentLocale.Current
MessageBox (currentLocale.Identifier) // show en_ES

Var x As Double = 22.5 // in debugger shows as 22,5
MessageBox x.ToString // show 22.5

TextFieldX.Text = x.ToString(Locale.Raw)
MessageBox TextFieldX.Text // show 22.5

x = TextFieldX.Text.ToDouble
MessageBox x.ToString // show 225

In the window TextFieldX shows as 22.5
In the debugger TextFieldX shows as 22.5 but
when x = TextFieldX.Text.ToDouble, x = 225

Documentation:
source = “123.45”
result = source.ToDouble ’ returns 123.45

https://www.dropbox.com/scl/fi/wwq8o7zwu5fwudtxxkke2/res.xojo_binary_project.zip?rlkey=jy1ka5jo22k64kwb10gduzxwz&dl=0

So I changed the code to use en_ES:

Var x As Double = 22.5
var en_ES as new Locale("en_ES")

MessageBox x.ToString(en_ES)

TextFieldX.Text = x.ToString(en_ES)

x = TextFieldX.Text.ToDouble
MessageBox x.ToString(en_ES)

results: 22,5 and 225.
Are you sure you see 22.5 and not 22,5?
Maybe it looks different if the OS is set to US or en_ES?
image

Edit: could it be that you changed the OS settings to show . instead of , for decimal separator? and converting 22,5 to 225? try with 2222.5

Because YOUR system is using “,” as decimal separator

Var x As Double = 22.5 // universal. Xojo uses only "." at code. OK

MessageBox x.ToString(Locale.Raw) + " and " + x.ToString() // 22.5 and 22,5

What you did here is incorrect in Xojo:

x = TextFieldX.Text.ToDouble // It will use your system decimal separator ",": wrong!
MessageBox x.ToString(Locale.Raw) // Now you got it broken

Xojo uses String.Val() to use point as decimal regardless system, so let’s fix it:

x = TextFieldX.Text.Val // Correct, convert "22.5" regardless system decimal separator
MessageBox x.ToString(Locale.Raw) // x is still correct now

Supposing that it is your intention… working with decimal point everywhere.

Ok, thanks, I was sorprised because I believe my original code worked on older versions.