setlocale

setlocale(int $category, ?string $locales, string ...$rest): string|false

Sets the process-wide locale (the global school): categories such as LC_ALL/LC_NUMERIC/LC_TIME/LC_COLLATE govern subsequent libc behaviour; returns the locale that actually took effect, or false on failure. After a successful de_DE switch printf's decimal point becomes a comma — always restore afterwards.

C C23

C23
#include <locale.h>
#include <stdio.h>

int main(void) {
    /* setlocale switches the locale of the whole PROCESS; "C" is the guaranteed baseline */
    printf("%s\n", setlocale(LC_NUMERIC, "C"));              /* always succeeds -> "C" */
    printf("%.1f\n", 1.5);                                    /* C locale: dot */
    printf("%s\n", setlocale(LC_NUMERIC, "xx_YY") ? "ok" : "NULL"); /* unknown: NULL */
    printf("%.1f\n", 1.5);                                    /* still the C behaviour */
    printf("%s\n", localeconv()->decimal_point);              /* localeconv() mirrors LC_NUMERIC */
    return 0;
}
Output
C
1.5
NULL
1.5
.

C++ C++26

C++26
#include <clocale>
#include <cstdio>

int main() {
    // C++ inherits C's setlocale: process-wide state; streams need explicit imbue()
    std::printf("%s\n", std::setlocale(LC_NUMERIC, "C"));              // -> "C"
    std::printf("%.1f\n", 1.5);                                         // C locale: dot
    std::printf("%s\n", std::setlocale(LC_NUMERIC, "xx_YY") ? "ok" : "NULL"); // unknown: NULL
    std::printf("%.1f\n", 1.5);                                         // still the C behaviour
    return 0;
}
Output
C
1.5
NULL
1.5

C# v11.0.100

v11.0.100
// C#: CultureInfo rides along per call or per thread (CurrentCulture), never global
using System.Globalization;

Console.WriteLine((1.5).ToString("0.0", CultureInfo.GetCultureInfo("de-DE")));
Console.WriteLine((1.5).ToString("0.0", CultureInfo.GetCultureInfo("en-US")));
CultureInfo.CurrentCulture = CultureInfo.GetCultureInfo("de-DE"); // thread-level only
Console.WriteLine((1.5).ToString("0.0"));
CultureInfo.CurrentCulture = CultureInfo.GetCultureInfo("en-US"); // restore
Console.WriteLine((1.5).ToString("0.0"));
Output
1,5
1.5
1,5
1.5

Go v1.27.4

v1.27.4
package main

import (
	"fmt"
	"strconv"
	"strings"
)

// Go has NO global locale: pass the locale around explicitly (the parameterised school)
var decimalSep = map[string]byte{"en-US": '.', "de-DE": ','}

func format1(v float64, locale string) string {
	s := strconv.FormatFloat(v, 'f', 1, 64)
	if sep := decimalSep[locale]; sep != '.' {
		s = strings.ReplaceAll(s, ".", string(sep))
	}
	return s
}

func main() {
	fmt.Println(format1(1.5, "en-US"))
	fmt.Println(format1(1.5, "de-DE"))
}
Output
1.5
1,5

Java v27.0.1

v27.0.1
import java.text.NumberFormat;
import java.util.Locale;

public class Main {
    public static void main(String[] args) {
        // Java: Locale is passed per call; only setDefault() flips JVM-wide state
        System.out.println(NumberFormat.getNumberInstance(Locale.GERMANY).format(1.5));
        System.out.println(NumberFormat.getNumberInstance(Locale.US).format(1.5));
        Locale.setDefault(Locale.Category.FORMAT, Locale.GERMANY); // per-process default
        System.out.println(NumberFormat.getNumberInstance().format(1.5));
        Locale.setDefault(Locale.Category.FORMAT, Locale.US);      // restore
        System.out.println(NumberFormat.getNumberInstance().format(1.5));
    }
}
Output
1,5
1.5
1,5
1.5

JavaScript v26.13.1

v26.13.1
// JS has NO global locale state: every Intl.* call takes the locale as a parameter
console.log(new Intl.NumberFormat('en-US').format(1.5)); // per-call locale choice
console.log(new Intl.NumberFormat('de-DE').format(1.5)); // nothing global is touched
console.log((1.5).toString());                            // plain conversion: always locale-free
Output
1.5
1,5
1.5

PHP v8.6.0

v8.6.0
// setlocale switches the locale of the whole PROCESS; 'C' is the guaranteed baseline
var_dump(setlocale(LC_NUMERIC, 'C'));        // always succeeds, returns "C"
printf("%.1f\n", 1.5);                        // C locale: dot is the decimal point
var_dump(setlocale(LC_NUMERIC, 'xx_YY'));     // unknown locale: false, state untouched
printf("%.1f\n", 1.5);                        // still the C behaviour
echo localeconv()['decimal_point'], "\n";     // localeconv() mirrors LC_NUMERIC
Output
string(1) "C"
1.5
bool(false)
1.5
.

Python v3.15.2

v3.15.2
import locale

# setlocale switches the locale of the whole PROCESS; 'C' is the guaranteed baseline
print(locale.setlocale(locale.LC_NUMERIC, 'C'))    # always succeeds -> 'C'
print(locale.format_string('%.1f', 1.5))            # C locale: dot is the decimal point
try:
    locale.setlocale(locale.LC_NUMERIC, 'xx_YY')    # unknown locale: raises Error
except locale.Error:
    print('unknown locale: locale.Error')
print(locale.format_string('%.1f', 1.5))            # still the C behaviour
print(locale.localeconv()['decimal_point'])         # localeconv() mirrors LC_NUMERIC
Output
C
1.5
unknown locale: locale.Error
1.5
.

Rust v1.99.1

v1.99.1
// Rust has NO global locale in std: pass conventions around explicitly
fn format_1(v: f64, decimal_sep: char) -> String {
    format!("{v:.1}").replace('.', &decimal_sep.to_string())
}

fn main() {
    println!("{}", format_1(1.5, '.'));
    println!("{}", format_1(1.5, ','));
}
Output
1.5
1,5

TypeScript v26.13.1

v26.13.1
// TS has NO global locale state either: Intl takes the locale as a parameter
const us: Intl.NumberFormat = new Intl.NumberFormat('en-US');
const de: Intl.NumberFormat = new Intl.NumberFormat('de-DE');
console.log(us.format(1.5)); // per-call locale choice
console.log(de.format(1.5)); // nothing global is touched
console.log((1.5).toString()); // plain conversion: always locale-free
Output
1.5
1,5
1.5

Language differences

PHP: setlocale is global process state (manual Warning: not thread-safe — other scripts can change it mid-run under threaded SAPIs; per-thread on Windows since 7.0.5). Category constants LC_ALL/LC_CTYPE/LC_NUMERIC/LC_TIME/LC_COLLATE/LC_MONETARY/LC_MESSAGES toggle libc behaviour for numbers, time, sorting, etc.; passing '0' (string) queries without changing, and multiple candidate names are tried in order (array form likewise). Always check the return value: false means the locale is not installed (list them with locale -a on Linux); the authoritative read of number formats is the localeconv() array (decimal_point/thousands_separator/grouping — demoed at the end of the example). Teaching point: under de_DE, printf('%.1f', 1.5) prints 1,5 — pin LC_NUMERIC back to 'C' before concatenating into SQL or round-tripping floats; the example uses xx_YY to demo the "failure leaves state unchanged" branch without depending on whether de_DE is installed. Versions: passing integer 0 as locales throws TypeError since 8.5 (use the string '0').

C: the same family as setlocale(3) — literally the same libc call; "C" is the baseline ISO C guarantees (strcoll degrades to strcmp under it), and unknown names return NULL. Newer language designs mostly abandoned the global locale.

C++: inherits the C API (std::setlocale); iostreams only notice it after an explicit imbue(std::locale), and std::locale::global affects C++ streams but not printf. Production code passes std::locale objects around instead.

C#: the per-object/per-thread school — CultureInfo rides along per call (ToString("0.0", culture)) or per thread (CurrentCulture); there is no process-wide switch, and the data ships with .NET, independent of OS-installed locales.

Go: no global locale — fmt/strconv always emit a dot; localization means explicit parameters or golang.org/x/text.

Java: Locale is passed per call; Locale.setDefault(Category, l) is a JVM-wide default (shared across threads — change with care). The JDK ships its own locale data (not ICU, not the OS).

JS/TS: no global state — every Intl.* call takes the locale as a parameter (ES Intl is ICU's sibling binding); plain toString is always locale-free.

Python: locale.setlocale is a direct translation of the C global school (process-wide, not thread-safe); locale.Error signals a missing locale; locale-aware format_string/strxfrm all depend on OS locales; modern projects prefer Babel (ICU family).

Rust: no locale in std; crates like pure-rust-locale/unic-langid provide the explicit-parameter school.

Side-by-side: PHP/C/Python are the global-process school (implicit and convenient but dangerous); C#/Java/JS/TS/Go/Rust are the parameterised school (the locale travels with the object/call) — always prefer the latter in new code.