Making format/2;3 deterministic when formatting numbers

I am using format/2;3 to output numeric data to be fed into a foreign system, which is a bit strict on the characters used as a decimal point and thousands separator.
e.g. the number -9993.81 shall be formatted -9,993.81

With the following code, prolog will produce the expected value:

?- format('~2:f', -9993.81).
-9,993.81
true.

However the manual at Predicate format/2 says:
“f: Floating point in non-exponential notation. The numeric argument defines the number of digits right of the decimal point. If the colon modifier ( : ) is used, the float is formatted using conventions from the current locale, which may define the decimal point as well as grouping of digits left of the decimal point.”
This essentially makes the output dependant on the locale the user happens to have defined in his environment.
Luckily for me this fits well, but changing the settings in my shell may render my program unusable.
Is there a way to avoid this (e.g. a supplemental flag to ~f, or a prolog flag)?

easy peasy…

?- setlocale(numeric,X,X).
X = 'it_IT.UTF-8'.

?- format('~2:f', -9993.81).
-9.993,81
true.

?- setlocale(numeric,X,'en_US.UTF-8').
X = 'it_IT.UTF-8'.

?- format('~2:f', -9993.81).
-9,993.81
true.

I think, fiddling the locale has consequences on reading as well as writing… add this obvious warning to what the doc page states…

Not that easy peasy:
On my mac, I have:

?- setlocale(numeric,X,X).
X = 'en_US.UTF-8'.

?- format('~2:f', -9993.81).
-9,993.81
true.

?- setlocale(numeric,X,'it_IT.UTF-8').
X = 'en_US.UTF-8'.

?- format('~2:f', -9993.81).
-9993,81
true.

(note the missing thousands separator)
As you can see, the locale “it_IT.UTF-8” on my mac yields another result as yours.

See SWI-Prolog -- Manual, which allows you to create an explicit locale and set is as default for a thread or even use it on a specific stream. That should make the outcome predictable. Notable locale_create/3

Yeah, this seems to work.
However note that locale_create/3 expects a string in its 2nd argument, whereas setlocale/3 returns an atom.

?- setlocale(numeric, X, X), locale_create(L, X, []).
ERROR: locale `'en_US.UTF-8'' does not exist
ERROR: In:
ERROR:   [11] locale_create(_10072,'en_US.UTF-8',[])
ERROR:    [9] toplevel_call(user:user: ...) at /Applications/SWI-Prolog.app/Contents/swipl/boot/toplevel.pl:1113
ERROR: 
ERROR: Note: some frames are missing due to last-call optimization.
ERROR: Re-run your program in debug mode (:- debug.) to get more detail.

Ok. Pushed a change to allow for an atom.