number_format

number_format(float $num, int $decimals = 0, ?string $decimal_separator = ".", ?string $thousands_separator = ","): string

Formats a number with grouped thousands and a fixed number of decimal places.

C C23

C23
#include <stdio.h>
#include <string.h>
int main(void) {
    char buf[32];
    snprintf(buf, sizeof buf, "%.2f", 1234567.891); // take two decimal digits first
    char *dot = strchr(buf, '.');
    int ilen = dot ? (int)(dot - buf) : (int)strlen(buf);
    char out[40];
    int o = 0;
    int first = ilen % 3; // digit count before the first comma
    for (int i = 0; i < ilen; i++) {
        if (i > 0 && (i - first) % 3 == 0) out[o++] = ','; // insert a comma every 3 digits
        out[o++] = buf[i];
    }
    if (dot) strcpy(out + o, dot); // then append the fractional part
    else out[o] = '\0';
    printf("%s\n", out);
    return 0;
}
Output
1,234,567.89

C++ C++26

C++26
#include <cstdio>
#include <cstring>
int main() {
    char buf[32];
    std::snprintf(buf, sizeof buf, "%.2f", 1234567.891); // "1234567.89"
    char *dot = std::strchr(buf, '.');
    int ilen = dot ? (int)(dot - buf) : (int)std::strlen(buf);
    char out[40];
    int o = 0;
    int first = ilen % 3; // digit count before the first comma
    for (int i = 0; i < ilen; i++) {
        if (i > 0 && (i - first) % 3 == 0) out[o++] = ','; // insert a comma every 3 digits
        out[o++] = buf[i];
    }
    if (dot) std::strcpy(out + o, dot);
    else out[o] = '\0';
    std::printf("%s\n", out);
    return 0;
}
Output
1,234,567.89

C# v11.0.100

v11.0.100
Console.WriteLine((1234567.891).ToString("N2",
    System.Globalization.CultureInfo.InvariantCulture));
Output
1,234,567.89

Go v1.27.4

v1.27.4
package main

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

func numberFormat(n float64, decimals int) string {
    s := strconv.FormatFloat(n, 'f', decimals, 64)
    parts := strings.SplitN(s, ".", 2)
    intPart := parts[0]
    sign := ""
    if strings.HasPrefix(intPart, "-") {
        sign = "-"
        intPart = intPart[1:]
    }
    var b strings.Builder
    b.WriteString(sign)
    for i, c := range intPart {
        if i > 0 && (len(intPart)-i)%3 == 0 {
            b.WriteByte(',')
        }
        b.WriteRune(c)
    }
    if len(parts) == 2 {
        b.WriteByte('.')
        b.WriteString(parts[1])
    }
    return b.String()
}

func main() {
    fmt.Println(numberFormat(1234567.891, 2))
}
Output
1,234,567.89

Java v27.0.1

v27.0.1
import java.util.Locale;

public class Main {
    public static void main(String[] args) {
        System.out.printf(Locale.US, "%,.2f%n", 1234567.891);
    }
}
Output
1,234,567.89

JavaScript v26.13.1

v26.13.1
console.log((1234567.891).toLocaleString("en-US", { minimumFractionDigits: 2, maximumFractionDigits: 2 }))
Output
1,234,567.89

PHP v8.6.0

v8.6.0
echo number_format(1234567.891, 2, ".", ",");
Output
1,234,567.89

Python v3.15.2

v3.15.2
print(f"{1234567.891:,.2f}")
Output
1,234,567.89

Rust v1.99.1

v1.99.1
// no built-in thousands formatting in the stdlib; hand-written grouping (negatives included), same idea as the Go version
fn number_format(n: f64, decimals: usize) -> String {
    let s = format!("{:.*}", decimals, n); // format with `decimals` fractional digits first
    let mut parts = s.splitn(2, '.');
    let int_part = parts.next().unwrap();
    let frac_part = parts.next();
    // strip the sign for negatives; grouping applies to the digits only
    let (sign, digits) = match int_part.strip_prefix('-') {
        Some(d) => ("-", d),
        None => ("", int_part),
    };
    let mut out = String::new();
    out.push_str(sign);
    for (i, c) in digits.chars().enumerate() {
        if i > 0 && (digits.len() - i) % 3 == 0 {
            out.push(','); // insert a comma every 3 digits
        }
        out.push(c);
    }
    if let Some(frac) = frac_part {
        out.push('.');
        out.push_str(frac);
    }
    out
}

fn main() {
    println!("{}", number_format(1234567.891, 2));
    println!("{}", number_format(-9876543.2, 2)); // negative number demo
}
Output
1,234,567.89
-9,876,543.20

TypeScript v26.13.1

v26.13.1
console.log((1234567.891).toLocaleString("en-US", { minimumFractionDigits: 2, maximumFractionDigits: 2 }));
Output
1,234,567.89

Language differences

C: has no built-in containers or higher-order functions; the example uses fixed arrays/buffers and hand-written loops, covering positive fixed-point formatting — negatives and custom separators are not implemented.

Go: the standard library has no built-in thousands grouping — the example hand-rolls it with the standard library only (in production use golang.org/x/text/message).

Java: needs Locale.US for stable separators and decimal points.

JavaScript: toLocaleString supports it natively.

PHP: supports thousands grouping natively.

Python: :,.2f supports it natively.

Rust: the standard library has no built-in thousands grouping — the example hand-rolls it (negatives included), same approach as the Go version.