Classic: How is Val() expected to work on small numbers?

Just had a nasty bug reported in my app that I tracked to storing decimal numbers in a prefs file.

In English setups, they store like this:
0.8789

In Germany, they store as
0,8789

So something is language sensitive.

When I read it back and use Val() to get the value, in Germany it barfs at the , and gives me a value of 0 instead of the float.
How can I avoid this?

Number –> String
– Format and CStr: localized (will store 0.8789 on English setup and 0,8789 on Germany setup)
– Str: non-localized (stores always 0.8789)

String –> Number
– CDouble: localized (will recognize 0.8789 on English setup and 0,8789 on Germany setup)
– Val: non-localized (will always only recognize 0.8789)

[quote=180327:@Jeff Tullin]Just had a nasty bug reported in my app that I tracked to storing decimal numbers in a prefs file.

In English setups, they store like this:
0.8789

In Germany, they store as
0,8789

So something is language sensitive.

When I read it back and use Val() to get the value, in Germany it barfs at the , and gives me a value of 0 instead of the float.
How can I avoid this?[/quote]

MyNumericVariable = Val(Replaceall(MyStringNumber,",","."))

For larger numbers and depending on where the string comes from, you may need to replace space by “” as in Europe, the thousand separator is often space, as in 1 123 123,12. Actually, Val is an issue in the US as well for large numbers formatted with comma thousand separators such as 1,123,123.12.

This is spot on the remarks Geoff made about Val and possible errors it does not raise, and instead returns zero. See http://www.xojo.com/blog/en/2015/04/ch-ch-ch-ch-changes.php

Also related: http://www.xojo.com/blog/en/2014/07/storing-monetary-values-in-xojo.php

[quote=180327:@Jeff Tullin]Just had a nasty bug reported in my app that I tracked to storing decimal numbers in a prefs file.

In English setups, they store like this:
0.8789

In Germany, they store as
0,8789

So something is language sensitive.

When I read it back and use Val() to get the value, in Germany it barfs at the , and gives me a value of 0 instead of the float.
How can I avoid this?[/quote]

Dont use format to write strings to prefs as you get localized decimal and thousands separators

Stupidly, I thought that since Format() has a format parameter, and I was using “0.000” then that’s what I would get.
Whats the point of a format method with a formatting parameter if it gets ignored? :frowning:

But yes, I have now got round this using Str
Problem solved.

http://documentation.xojo.com/index.php/Format

Perhaps where it says

Although the special formatting characters are U.S. characters, the actual characters that will appear are based on the current operating system settings. For example, Windows uses the settings in the user’s Regional and Language Options Control Panel. Formatting characters are specified in similar ways on other operating systems.

Format is “internationally savvy” where str uses US formatting no matter what so you can reliably store data for machine readability

Agreed, thats what it says.
All I’m saying is that was a huge surprise.

Finding that asking for 0.0000 might produce 0,0000 was like asking for ice cream and getting mustard because ‘thats what we like to eat around here’

The format string is so fundamental to Format (which I have been using to format numbers in this way for a loooonnng time) that I didn’t actually go and read the small print when I wrote the code.

I know now. Not arguing about it. Just really really surprised at the behavior.

[quote=180394:@Jeff Tullin]Agreed, thats what it says.
All I’m saying is that was a huge surprise.

Finding that asking for 0.0000 might produce 0,0000 was like asking for ice cream and getting mustard because ‘thats what we like to eat around here’

The format string is so fundamental to Format (which I have been using to format numbers in this way for a loooonnng time) that I didn’t actually go and read the small print when I wrote the code.

I know now. Not arguing about it. Just really really surprised at the behavior.[/quote]

The new framework Double.ToText allows choosing the locale.
http://developer.xojo.com/double