round

round(int|float $num, int $precision = 0, int $mode = PHP_ROUND_HALF_UP): float

Rounds a float to a given precision.

C C23

C23
#include <math.h>
#include <stdio.h>
int main(void) {
    printf("%.0f\n", round(2.6));
    printf("%.2f\n", 3.14159); // precision: 2 decimals
    printf("%.0f %.0f\n", round(2.5), round(-2.5)); // C round(): half away from zero
    return 0;
}
Output
3
3.14
3 -3

C++ C++26

C++26
#include <cmath>
#include <cstdio>
int main() {
    std::printf("%.0f\n", std::round(2.6));
    std::printf("%.2f\n", 3.14159); // precision: 2 decimals
    std::printf("%.0f %.0f\n", std::round(2.5), std::round(-2.5)); // half away from zero
    return 0;
}
Output
3
3.14
3 -3

C# v11.0.100

v11.0.100
Console.WriteLine(Math.Round(2.6));
Console.WriteLine(Math.Round(3.14159, 2)); // precision: 2 decimals
// C# rounds .5 to even by default: opt in to AwayFromZero explicitly
Console.WriteLine($"{Math.Round(2.5, MidpointRounding.AwayFromZero)} {Math.Round(-2.5, MidpointRounding.AwayFromZero)}");
Output
3
3.14
3 -3

Go v1.27.4

v1.27.4
package main

import (
    "fmt"
    "math"
)

func main() {
    fmt.Println(math.Round(2.6))

	fmt.Printf("%.2f\n", 3.14159)                  // precision: 2 decimals
	fmt.Println(math.Round(2.5), math.Round(-2.5)) // half away from zero
}
Output
3
3.14
3 -3

Java v27.0.1

v27.0.1
public class Main {
    public static void main(String[] args) {
        System.out.println(Math.round(2.6));
        System.out.printf("%.2f%n", 3.14159); // precision: 2 decimals
        // Java rounds .5 toward positive infinity: -2.5 becomes -2, unlike PHP
        System.out.println(Math.round(2.5) + " " + Math.round(-2.5));
    }
}
Output
3
3.14
3 -2

JavaScript v26.13.1

v26.13.1
console.log(Math.round(2.6))
console.log((3.14159).toFixed(2)) // precision: 2 decimals
console.log(Math.round(2.5), Math.round(-2.5)) // .5 rounds toward +Infinity
Output
3
3.14
3 -2

PHP v8.6.0

v8.6.0
echo round(2.6), "\n";
// precision argument: 2 decimal places; .5 boundaries round half away from zero
echo round(3.14159, 2), "
";
echo round(2.5), " ", round(-2.5);
Output
3
3.14
3 -3

Python v3.15.2

v3.15.2
print(round(2.6))
print(round(3.14159, 2)) # precision: 2 decimals
print(round(2.5), round(-2.5)) # banker's rounding: .5 goes to even
Output
3
3.14
2 -2

Rust v1.99.1

v1.99.1
fn main() {
    println!("{}", 2.6f64.round());

    println!("{:.2}", 3.14159_f64); // precision: 2 decimals
    println!("{} {}", 2.5_f64.round(), (-2.5_f64).round()); // half away from zero
}
Output
3
3.14
3 -3

TypeScript v26.13.1

v26.13.1
console.log(Math.round(2.6));
console.log((3.14159).toFixed(2)); // precision: 2 decimals
console.log(Math.round(2.5), Math.round(-2.5)); // .5 rounds toward +Infinity
Output
3
3.14
3 -2

Language differences

C: rounds with round() and controls the format via printf precision.

C#: Math.Round defaults to bankers rounding (to even on .5); 2.6 in the example is unaffected.

Go: math.Round rounds ties away from zero.

Java: Math.round rounds ties toward positive infinity (-2.5 gives -2).

JavaScript: Math.round rounds ties toward positive infinity (-2.5 gives -2).

PHP: round rounds ties away from zero.

Python: 3's round uses banker's rounding (ties to even), so round(2.5) gives 2.

Rust: round rounds ties away from zero.