How to Round Number to n Decimal Places in PHP ? Last Updated : 17 Jan, 2024 Comments Improve Suggest changes Like Article Like Report Rounding numbers to a specific decimal place is a common operation in programming, especially when dealing with precision in mathematical calculations. PHP provides several approaches to achieve this rounding. Table of Content Using round() FunctionUsing number_format() FunctionUsing sprintf() FunctionUsing round() FunctionThe round() function is a built-in PHP function that rounds a number to the nearest integer or a specified number of decimal places. PHP <?php $number = 3.14159; // Round to 2 decimal places $roundNumber = round($number, 2); echo "Original Number: " . $number . "\n"; echo "Rounded Number: " . $roundNumber; ?> OutputOriginal Number: 3.14159 Rounded Number: 3.14Using number_format() FunctionThe number_format() function is used to formatting numbers as strings with grouped thousands. However, it can also be utilized to round numbers to a specific number of decimal places. PHP <?php $number = 3.14559; // Round to 2 decimal places $roundNumber = number_format($number, 2); echo "Original Number: " . $number . "\n"; echo "Rounded Number: " . $roundNumber; ?> OutputOriginal Number: 3.14559 Rounded Number: 3.15Using sprintf() FunctionThe sprintf() function is used to formatting strings, and it can also be employed for rounding numbers to a specific number of decimal places. PHP <?php $number = 3.14559; // Round to 2 decimal places $roundNumber = sprintf("%.2f", $number); echo "Original Number: " . $number . "\n"; echo "Rounded Number: " . $roundNumber; ?> OutputOriginal Number: 3.14559 Rounded Number: 3.15 Comment More infoAdvertise with us Next Article Round off a number upto 2 decimal place using JavaScript V vkash8574 Follow Improve Article Tags : PHP Geeks Premier League 2023 PHP-Number Similar Reads How to Round Numbers in Excel? Precision is important, but so is clarity. Whether you're preparing a financial report, managing inventory, or analyzing sales data, knowing how to round numbers in Excel can streamline your spreadsheets and make your data easier to understand. Excel offers several functions tailored for different r 13 min read Format Number of Decimal Places in R In this article we are going to discuss how to format numbers up to n decimal places in the R programming language. In R language, the decimal number is represented by . symbol Method 1: Format() function Format() function can be used to format decimal values by rounding them and displaying only a s 3 min read Round off a number upto 2 decimal place using JavaScript A number can be rounded off to 2 decimal places using 2 approaches: Method 1: Using the toFixed() method The toFixed() method is used to return a string representation of a number upto a certain number of digits after the decimal point. The number is rounded and the fractional part is padded if nece 2 min read How to convert a String into Number in PHP ? Strings in PHP can be converted to numbers (float/ int/ double) very easily. In most use cases, it won't be required since PHP does implicit type conversion. This article covers all the different approaches for converting a string into a number in PHP, along with their basic illustrations.There are 4 min read How to convert a String into Number in PHP ? Strings in PHP can be converted to numbers (float/ int/ double) very easily. In most use cases, it won't be required since PHP does implicit type conversion. This article covers all the different approaches for converting a string into a number in PHP, along with their basic illustrations.There are 4 min read How to extract Numbers From a String in PHP ? Extracting numbers from a string involves identifying and isolating numerical values embedded within a text. This process can be done using programming techniques, such as regular expressions, to filter out and retrieve only the digits from the string, ignoring all other characters.Here we have some 3 min read Like