Subscribe Bookmark RSS Feed

Effect of local PC settings on parsing text

An interesting little problem arose recently in the context of a script that is required to run in several locations around the world. Solving the immediate problem is straightforward enough, but I wondered whether there's a more general solution.

The problem was caused by some of the PCs on which the script was being run having different settings for their number formats. In this instance the decimal point in one location had been set to "·" (ASCII code 250) as opposed to the usual one on a UK keyboard ("."). In Windows 7 this can be changed in Control Panel | Clock Language and Region | Change the Date, Time or Number Format | Additional Settings | Decimal Symbol.

The effect was that any line involving an eval(parse(...)) construction crashed the program, as this trivial example shows:

A = 123.456; B = 1.0;
TextToParse = "C = " || char(A+B);
eval(parse(TextToParse));

Unexpected "·456". Perhaps there is a missing ";" or ",".
Line 1 Column 8: C = 124►·456

The remaining text that was ignored was
·456
124

To sort this out obviously all I need to do is write a small function of my own to replace the "char" function, looking for any unexpected characters in a text field which I know should be a number, and replacing them with the normal decimal point. However, I'd like to know if there's a universal fixit to interpret correctly something more complicated like a user with Arabic or Cyrillic numerals, or negative numbers being displayed in parenthesis. Does anybody know if there is?

(Incidentally, I've tried changing other settings within the "Additional Settings" screen without encountering any difficulties: the decimal point is the only one I've found that causes trouble so far, but I imagine there could potentially be others.)
2 REPLIES
Here's the sort of function I had in mind, though I imagine it can be shortened:






style="font-size: 9pt; font-family: "Courier New"; color: black;">textfromnumber

style="font-size: 9pt; font-family: "Courier New"; color: navy;">=

style="font-size: 9pt; font-family: "Courier New"; color: black;">

style="font-size: 9pt; font-family: "Courier New"; color: rgb(0, 0, 221);">function

style="font-size: 9pt; font-family: "Courier New"; color: black;">({

style="font-size: 9pt; font-family: "Courier New"; color: black;">x}

style="font-size: 9pt; font-family: "Courier New"; color: navy;">,

style="font-size: 9pt; font-family: "Courier New"; color: black;">
{default
local}

style="font-size: 9pt; font-family: "Courier New"; color: navy;">,

style="font-size: 9pt; font-family: "Courier New"; color: black;">





style="font-size: 9pt; font-family: "Courier New"; color: black;">
style="">      
charx

style="font-size: 9pt; font-family: "Courier New"; color: navy;">=

style="font-size: 9pt; font-family: "Courier New"; color: black;">

style="font-size: 9pt; font-family: "Courier New"; color: rgb(0, 0, 221);">char

style="font-size: 9pt; font-family: "Courier New"; color: black;">(

style="font-size: 9pt; font-family: "Courier New"; color: black;">x)

style="font-size: 9pt; font-family: "Courier New"; color: navy;">;

style="font-size: 9pt; font-family: "Courier New"; color: black;">





style="font-size: 9pt; font-family: "Courier New"; color: black;">
style="">      
validchars

style="font-size: 9pt; font-family: "Courier New"; color: navy;">=

style="font-size: 9pt; font-family: "Courier New"; color: black;">

style="font-size: 9pt; font-family: "Courier New"; color: purple;">"-0123456789."

style="font-size: 9pt; font-family: "Courier New"; color: navy;">;

style="font-size: 9pt; font-family: "Courier New"; color: black;">





style="font-size: 9pt; font-family: "Courier New"; color: black;">
style="">      
newcharx

style="font-size: 9pt; font-family: "Courier New"; color: navy;">=

style="font-size: 9pt; font-family: "Courier New"; color: black;">

style="font-size: 9pt; font-family: "Courier New"; color: purple;">""

style="font-size: 9pt; font-family: "Courier New"; color: navy;">;

style="font-size: 9pt; font-family: "Courier New"; color: black;">





style="font-size: 9pt; font-family: "Courier New"; color: black;">
style="">      

style="font-size: 9pt; font-family: "Courier New"; color: rgb(0, 0, 221);">for

style="font-size: 9pt; font-family: "Courier New"; color: black;">(

style="font-size: 9pt; font-family: "Courier New"; color: black;">i

style="font-size: 9pt; font-family: "Courier New"; color: navy;">=

style="font-size: 9pt; font-family: "Courier New"; color: teal;">1

style="font-size: 9pt; font-family: "Courier New"; color: navy;">,

style="font-size: 9pt; font-family: "Courier New"; color: black;">
i

style="font-size: 9pt; font-family: "Courier New"; color: navy;"><=

style="font-size: 9pt; font-family: "Courier New"; color: rgb(0, 0, 221);">length

style="font-size: 9pt; font-family: "Courier New"; color: black;">(

style="font-size: 9pt; font-family: "Courier New"; color: black;">charx)

style="font-size: 9pt; font-family: "Courier New"; color: navy;">,

style="font-size: 9pt; font-family: "Courier New"; color: black;">
i

style="font-size: 9pt; font-family: "Courier New"; color: navy;">++,

style="font-size: 9pt; font-family: "Courier New"; color: black;">





style="font-size: 9pt; font-family: "Courier New"; color: black;">
style="">             
testchar

style="font-size: 9pt; font-family: "Courier New"; color: navy;">=

style="font-size: 9pt; font-family: "Courier New"; color: black;">

style="font-size: 9pt; font-family: "Courier New"; color: rgb(0, 0, 221);">substr

style="font-size: 9pt; font-family: "Courier New"; color: black;">(

style="font-size: 9pt; font-family: "Courier New"; color: black;">charx

style="font-size: 9pt; font-family: "Courier New"; color: navy;">,

style="font-size: 9pt; font-family: "Courier New"; color: black;">
i

style="font-size: 9pt; font-family: "Courier New"; color: navy;">,

style="font-size: 9pt; font-family: "Courier New"; color: black;">

style="font-size: 9pt; font-family: "Courier New"; color: teal;">1

style="font-size: 9pt; font-family: "Courier New"; color: black;">)

style="font-size: 9pt; font-family: "Courier New"; color: navy;">;

style="font-size: 9pt; font-family: "Courier New"; color: black;">





style="font-size: 9pt; font-family: "Courier New"; color: black;">
style="">             

style="font-size: 9pt; font-family: "Courier New"; color: rgb(0, 0, 221);">if

style="font-size: 9pt; font-family: "Courier New"; color: black;">(

style="font-size: 9pt; font-family: "Courier New"; color: rgb(0, 0, 221);">contains

style="font-size: 9pt; font-family: "Courier New"; color: black;">(

style="font-size: 9pt; font-family: "Courier New"; color: black;">validchars

style="font-size: 9pt; font-family: "Courier New"; color: navy;">,

style="font-size: 9pt; font-family: "Courier New"; color: black;">
testchar)

style="font-size: 9pt; font-family: "Courier New"; color: navy;"><

style="font-size: 9pt; font-family: "Courier New"; color: black;">

style="font-size: 9pt; font-family: "Courier New"; color: teal;">1

style="font-size: 9pt; font-family: "Courier New"; color: navy;">,

style="font-size: 9pt; font-family: "Courier New"; color: black;">
testchar

style="font-size: 9pt; font-family: "Courier New"; color: navy;">=

style="font-size: 9pt; font-family: "Courier New"; color: black;">

style="font-size: 9pt; font-family: "Courier New"; color: purple;">"."

style="font-size: 9pt; font-family: "Courier New"; color: black;">)

style="font-size: 9pt; font-family: "Courier New"; color: navy;">;

style="font-size: 9pt; font-family: "Courier New"; color: black;">





style="font-size: 9pt; font-family: "Courier New"; color: black;">
style="">             
newcharx

style="font-size: 9pt; font-family: "Courier New"; color: navy;">=

style="font-size: 9pt; font-family: "Courier New"; color: black;">
newcharx

style="font-size: 9pt; font-family: "Courier New"; color: navy;">||

style="font-size: 9pt; font-family: "Courier New"; color: black;">
testchar





style="font-size: 9pt; font-family: "Courier New"; color: black;">
style="">             
)

style="font-size: 9pt; font-family: "Courier New"; color: navy;">;

style="font-size: 9pt; font-family: "Courier New"; color: black;">





style="font-size: 9pt; font-family: "Courier New"; color: black;">
style="">      
newcharx

style="font-size: 9pt; font-family: "Courier New"; color: navy;">;

style="font-size: 9pt; font-family: "Courier New"; color: black;">





style="font-size: 9pt; font-family: "Courier New"; color: black;">
style="">      
)

style="font-size: 9pt; font-family: "Courier New"; color: navy;">;

style="font-size: 9pt; font-family: "Courier New"; color: black;">





style="font-size: 9pt; font-family: "Courier New"; color: black;">

 





style="font-size: 9pt; font-family: "Courier New"; color: black;">No

style="font-size: 9pt; font-family: "Courier New"; color: navy;">=

style="font-size: 9pt; font-family: "Courier New"; color: black;">

style="font-size: 9pt; font-family: "Courier New"; color: teal;">123.456

style="font-size: 9pt; font-family: "Courier New"; color: navy;">;

style="font-size: 9pt; font-family: "Courier New"; color: black;">





style="font-size: 9pt; font-family: "Courier New"; color: rgb(0, 0, 221);">show

style="font-size: 9pt; font-family: "Courier New"; color: black;">(

style="font-size: 9pt; font-family: "Courier New"; color: rgb(0, 0, 221);">char

style="font-size: 9pt; font-family: "Courier New"; color: black;">(

style="font-size: 9pt; font-family: "Courier New"; color: black;">No))

style="font-size: 9pt; font-family: "Courier New"; color: navy;">;

style="font-size: 9pt; font-family: "Courier New"; color: black;">





style="font-size: 9pt; font-family: "Courier New"; color: rgb(0, 0, 221);">show

style="font-size: 9pt; font-family: "Courier New"; color: black;">(

style="font-size: 9pt; font-family: "Courier New"; color: black;">textfromnumber(No))

style="font-size: 9pt; font-family: "Courier New"; color: navy;">;

style="font-size: 9pt; font-family: "Courier New"; color: black;">



style="font-size: 9pt; font-family: "Courier New"; color: silver;">



style="font-size: 9pt; font-family: "Courier New"; color: black;">

 





style="font-size: 9pt; font-family: "Courier New"; color: black;">Char(::No):"123·456"

style="font-size: 9pt; font-family: "Courier New"; color: black;">





style="font-size: 9pt; line-height: 115%; font-family: "Courier New"; color: black;">"123.456"






ms

Super User

Joined:

Jun 23, 2011

I get exactly the same error in JMP (8 & 9) for Macintosh with my system set to use "," as decimal separator.

JSL works only with decimal numbers entered with "." but the format of the Char() output for some reason adapts to the system settings. And Parse() has then of course no clue if the comma represents a decimal point or an JSL argument separator.

I have not found a general solution. But here's another, slightly shorter, custom Char() function.



Char(A) = "123,456";
"123.456";