numbers as text with thousand separators cause error in tonumber() function

Hello

in my App I have a text label displaying the number 200,000 with the thousand separator ‘,’.

now when I read that by using tonumber(“200,000”).  it causes an error due to the comma.

Now to fix this I can use, string.gsub(“200,000”, ‘,’, ‘’).

​which will give me 200000.  Which is fine as long as my numbers use ‘,’ as the thousand separator.  

But what if my iPad or Tablet is set in a region that uses ‘.’ as the thousand separator, like Europe, is there a system parameter or global parameter that I can read something like in other languages number.thousandseparator = ‘,’ so I can detect if I need to switch out the ‘,’ or the ‘.’ in the large (thousand plus) number?

​So in region like US or UK I can use string.gsub(“200,000”, ‘,’, ‘’)

but in Europe I will need to use string.gsub(“200.000”, ‘.’, ‘’)

​I need some like os.setlocale() or os.getlocale() to tell me the locale thousand separator so I can use a simple variable to hold the locale thousand separator character.

str = “200,000”. or it could be “200.000” if I am in Europe.

localeSep = “,”  or “.” which I need to get from the system settings But how?

string.gsub(str, localeSep, ‘’)

regards

Bruce

You can remove all non-numeric characters from the string like this:

[lua]

str = string.gsub(str, “[^0-9]”, “”)

[/lua]

nick

yes but then what happens to the actual decimal point, you do not want to remove that, we only want to remove the thousand separator.

so for example 

tonumber(“200,000”). I agree this will work

tonumber(“1,010.25”) this solution will fail and give 101025, for a UK or USA region number

tonumber(“1.010,25”) this solution will fail again for a European region number.

thats why I was asking about replacing the thousand separator and how to find out what region you are in so you can decide if the thousand separator is a ‘,’ comma or ‘.’ a point as in the case of European countries.

regards

bruce

You could either set up a table that converts all the country codes into a dot or comma. Shouldn’t take too long to populate in Excel and use a formula to generate the Lua code.

[lua]

local separators = {}

separators[“US”] = “,”

separators[“DE”] = “.”

separators[“FR”] = " "

[/lua]

Alternatively, if your numbers are always to one/two decimal places (or none), this may work:

If there is a dot:

  One/Two characters after the dot = UK, USA. Normal tonumber() will work.

  More than two characters = Euro. Remove dot, replace comma with dot. Then tonumber().

No dot:

  One/Two characters after a comma = Euro. Replace comma with dot. Then tonumber().

  

  No comma or more than two characters after comma = UK, USA. Normal tonumber() will work.

Nick

OK, i was hoping that i would be able to get from the App the device settings, in relation to regional settings.  is this not possible?  this sort of thing applies to numbers and dates.

yes i can make a list as you suggest but again it may fail if the user is in one of the regions but the device settings is set to another region.

regards

bruce.

If it were me, I would just keep the original number stored in one variable as a number, and then only convert it to a separate string variable when you need to display it. If you really need to get the value from the text object directly, you can always add it as a separate property:

local myNumber = 200000 local function getString(num) return string.format(num, "comma\_stuff") end local myText = display.newText(getString(myNumber), x, y, etc) myText.value = myNumber

Edit: This way, you don’t need to worry about doing tonumber() on a string which has commas/periods, since you already have the number available as a number.

So from these replies and lack of any others it seems there is no way to get the regional settings for numbers from the OS.

if this is the case then maybe this is something that needs to be addressed in future versions.

Regards

Bruce.

Quite, I believe iOS and Android both have API calls available to get this data. Corona seems to have a habit of exposing just what they think we’ll need, rather than just exposing everything that is available to native developers.

Can someone file a feature request at http://feedback.coronalabs.com?

Rob

Rob

ok I have just submitted a feature request on this issue.

bruce

Rob, as a fairly small and lean company, can’t Corona be a bit more nimble and flexible than this?

It’s a quick and easy win, and I wouldn’t call it a feature - more an omission.

Instead we’ll have to wait 6 months until enough people stumble across the feedback page (I’ve been there a handful of times in six years of using Corona) to possibly vote it up - and let’s face it, most won’t care enough to vote for it, so it’ll probably never happen. That doesn’t mean it shouldn’t have been in the API to begin with.

Nick, one of the challenges of being small and lean, is there are simply not enough engineering hours in the day to do everything you and us want to do.  Something small like this won’t take a lot of votes, but it’s more having a way to track it.  I believe this will be easy to implement, but still that means an engineer isn’t working on some other task that you (the community) has already asked for.

Rob

You can remove all non-numeric characters from the string like this:

[lua]

str = string.gsub(str, “[^0-9]”, “”)

[/lua]

nick

yes but then what happens to the actual decimal point, you do not want to remove that, we only want to remove the thousand separator.

so for example 

tonumber(“200,000”). I agree this will work

tonumber(“1,010.25”) this solution will fail and give 101025, for a UK or USA region number

tonumber(“1.010,25”) this solution will fail again for a European region number.

thats why I was asking about replacing the thousand separator and how to find out what region you are in so you can decide if the thousand separator is a ‘,’ comma or ‘.’ a point as in the case of European countries.

regards

bruce

You could either set up a table that converts all the country codes into a dot or comma. Shouldn’t take too long to populate in Excel and use a formula to generate the Lua code.

[lua]

local separators = {}

separators[“US”] = “,”

separators[“DE”] = “.”

separators[“FR”] = " "

[/lua]

Alternatively, if your numbers are always to one/two decimal places (or none), this may work:

If there is a dot:

  One/Two characters after the dot = UK, USA. Normal tonumber() will work.

  More than two characters = Euro. Remove dot, replace comma with dot. Then tonumber().

No dot:

  One/Two characters after a comma = Euro. Replace comma with dot. Then tonumber().

  

  No comma or more than two characters after comma = UK, USA. Normal tonumber() will work.

Nick

OK, i was hoping that i would be able to get from the App the device settings, in relation to regional settings.  is this not possible?  this sort of thing applies to numbers and dates.

yes i can make a list as you suggest but again it may fail if the user is in one of the regions but the device settings is set to another region.

regards

bruce.

If it were me, I would just keep the original number stored in one variable as a number, and then only convert it to a separate string variable when you need to display it. If you really need to get the value from the text object directly, you can always add it as a separate property:

local myNumber = 200000 local function getString(num) return string.format(num, "comma\_stuff") end local myText = display.newText(getString(myNumber), x, y, etc) myText.value = myNumber

Edit: This way, you don’t need to worry about doing tonumber() on a string which has commas/periods, since you already have the number available as a number.

So from these replies and lack of any others it seems there is no way to get the regional settings for numbers from the OS.

if this is the case then maybe this is something that needs to be addressed in future versions.

Regards

Bruce.

Quite, I believe iOS and Android both have API calls available to get this data. Corona seems to have a habit of exposing just what they think we’ll need, rather than just exposing everything that is available to native developers.

Can someone file a feature request at http://feedback.coronalabs.com?

Rob