bcscale
bcscale(int $scale): int
Sets the global default scale (decimal places) for the bcmath family, effective when a scale argument is omitted; returns the previous scale since PHP 8.
C C23
#include <stdio.h>
/* C has no global precision mechanism: each printf format specifies it explicitly (rounding); fixed-point decimal needs a software library */
int main(void) {
printf("%.2f\n", 1.05 + 2.033);
printf("%.2f\n", 5.0 - 1.234);
printf("%.4f\n", 1.0 + 2.0);
printf("%.6f\n", 1.0 / 3.0);
return 0;
}
3.08 3.77 3.0000 0.333333
C++ C++26
#include <iomanip>
#include <iostream>
int main() {
// A stream's precision + std::fixed are sticky (the closest thing to "global" within the scope)
std::cout << std::fixed << std::setprecision(2) << 1.05 + 2.033 << "\n";
std::cout << 5.0 - 1.234 << "\n";
// setprecision stays in effect on the stream: later output inherits it until changed again
std::cout << std::setprecision(4) << 1.0 + 2.0 << "\n";
std::cout << 1.0 / 3.0 << "\n";
return 0;
}
3.08 3.77 3.0000 0.3333
C# v11.0.100
using System.Globalization;
// decimal: a fixed 28-29 significant digits, no global precision to tune (unlike bcmath's variable scale)
Console.WriteLine((1.05m + 2.033m).ToString("F2", CultureInfo.InvariantCulture));
Console.WriteLine((5m - 1.234m).ToString("F2", CultureInfo.InvariantCulture));
Console.WriteLine((1m + 2m).ToString("F4", CultureInfo.InvariantCulture));
Console.WriteLine((1m / 3m).ToString("F6", CultureInfo.InvariantCulture));
3.08 3.77 3.0000 0.333333
Go v1.27.4
package main
import (
"fmt"
"math/big"
)
func main() {
// big.Float.SetPrec: per-value binary mantissa precision — there is no global precision setting
x, _ := new(big.Float).SetPrec(16).SetString("1.05")
y, _ := new(big.Float).SetPrec(16).SetString("2.033")
fmt.Println(new(big.Float).Add(x, y).Text('f', 2))
a, _ := new(big.Float).SetPrec(16).SetString("5")
b, _ := new(big.Float).SetPrec(16).SetString("1.234")
fmt.Println(new(big.Float).Sub(a, b).Text('f', 2))
// Fixed-point decimal places need a third-party library (shopspring/decimal)
}
3.08 3.77
Java v27.0.1
import java.math.BigDecimal;
import java.math.MathContext;
public class Main {
public static void main(String[] args) {
// MathContext: explicitly passed significant-digit precision (HALF_EVEN rounding); no process-wide global setting
MathContext mc = new MathContext(2);
System.out.println(new BigDecimal("1.05").add(new BigDecimal("2.033"), mc));
System.out.println(new BigDecimal("5").subtract(new BigDecimal("1.234"), mc));
System.out.println(new BigDecimal("1").add(new BigDecimal("2"), new MathContext(4)));
System.out.println(new BigDecimal(1).divide(new BigDecimal(3), new MathContext(6)));
}
}
3.1 3.8 3 0.333333
JavaScript v26.13.1
// JS has no global precision mechanism: toFixed specifies it per call site (rounds, unlike bcmath's truncation)
console.log((Number("1.05") + Number("2.033")).toFixed(2));
console.log((Number("5") - Number("1.234")).toFixed(2));
console.log((Number("1") + Number("2")).toFixed(4));
// The binary floating-point pitfall: real-world decimal work uses libraries like decimal.js
console.log(0.1 + 0.2 === 0.3);
3.08 3.77 3.0000 false
PHP v8.6.0
// bcscale: sets the global default decimal places (applies when bcmath functions omit the scale argument afterwards; truncates, never rounds up)
bcscale(2);
echo bcadd("1.05", "2.033"), "\n";
echo bcsub("5", "1.234"), "\n";
// An explicit scale argument takes precedence over the global setting
echo bcadd("1", "2", 4), "\n";
// Since PHP 8 bcscale returns the previous global scale (int); the initial default is 0
$old = bcscale(6);
echo bcdiv("1", "3"), "\n";
echo "old scale: $old\n";
3.08 3.76 3.0000 0.333333 old scale: 2
Python v3.15.2
from decimal import Decimal, getcontext, localcontext
# decimal: getcontext().prec is a thread-local global precision — but it means "total significant digits" (not PHP's decimal places) and rounds
getcontext().prec = 2
print(Decimal("1.05") + Decimal("2.033"))
print(Decimal("5") - Decimal("1.234"))
# localcontext: a temporary override, restored automatically at scope end (more controllable than PHP's global mutable state)
with localcontext() as ctx:
ctx.prec = 4
print(Decimal("1") + Decimal("2"))
getcontext().prec = 6
print(Decimal(1) / Decimal(3))
3.1 3.8 3 0.333333
Rust v1.99.1
// No decimal type or global precision in the standard library: f64 + format! specified per call site (rounds); in real projects use rust_decimal
fn main() {
println!("{:.2}", 1.05 + 2.033);
println!("{:.2}", 5.0 - 1.234);
println!("{:.4}", 1.0 + 2.0);
// Precision is specified explicitly at every formatting site
println!("{:.6}", 1.0 / 3.0);
}
3.08 3.77 3.0000 0.333333
TypeScript v26.13.1
// JS has no global precision mechanism: toFixed specifies it per call site (rounds, unlike bcmath's truncation)
const sum: string = (Number("1.05") + Number("2.033")).toFixed(2);
console.log(sum);
console.log((Number("5") - Number("1.234")).toFixed(2));
console.log((Number("1") + Number("2")).toFixed(4));
// The binary floating-point pitfall: real-world decimal work uses libraries like decimal.js
console.log(0.1 + 0.2 === 0.3);
3.08 3.77 3.0000 false
Language differences
C: no global precision mechanism — the printf format string (%.2f) specifies it per call with rounding; bcmath-style fixed-point decimals need a software library.
C++: a stream's precision() plus std::fixed persists for that stream — the closest thing to "global" within one stream (setprecision(2) keeps applying until changed); still stream-object state rather than process-global.
C#: decimal has a fixed 28-29 significant digits, not tunable; ToString("F2") rounds; no global precision setting.
Go: big.Float precision is per value (SetPrec counts binary mantissa bits), nothing global; Text('f', 2) rounds; shopspring/decimal for fixed-point decimal work.
Java: MathContext(precision) is passed explicitly with each call (HALF_EVEN rounding) and means total significant digits, not decimal places; no process-global setting.
JavaScript: no global precision — toFixed(n) per call (it rounds); binary floats make 0.1+0.2 !== 0.3, use decimal.js/bignumber.js for decimal needs.
PHP: bcscale(n) is a process-global default scale; bcadd/bcsub/bcmul/bcdiv and friends omitting the scale argument then truncate to n places (no rounding); an explicit argument beats the global; since 8.0 it returns the previous value (int), 7.x returned true (bool); the initial value comes from the bcmath.scale ini — mutable global state is a footgun in long-running requests, so prefer passing scale per call or the BcMath\Number object API (8.0+).
Python: decimal.getcontext().prec is thread-local global precision, but it means total significant digits (1.05+2.033 gives 3.1, not PHP's 3.08) with default HALF_EVEN rounding; the localcontext() context manager overrides temporarily and restores on exit; quantize(Decimal("0.01")) is what controls decimal places.
Rust: no decimal type and no global precision in std — format!("{:.2}") per call (rounding); rust_decimal for real work (each value carries its own scale).
TypeScript: same as JavaScript.