PHP How to Round Down to Two Decimal Places

PHP How do I round down to two decimal places?

This can work: floor($number * 100) / 100

Show a number to two decimal places

You can use number_format():

return number_format((float)$number, 2, '.', '');

Example:

$foo = "105";
echo number_format((float)$foo, 2, '.', ''); // Outputs -> 105.00

This function returns a string.

Rounding up to the second decimal place

Check out http://www.php.net/manual/en/function.round.php

<?php

echo round(3.6451895227869, 2);

?>

EDIT
Try using this custom function http://www.php.net/manual/en/function.round.php#102641

<?php 
function round_up ( $value, $precision ) {
$pow = pow ( 10, $precision );
return ( ceil ( $pow * $value ) + ceil ( $pow * $value - ceil ( $pow * $value ) ) ) / $pow;
}

echo round_up(3.63333333333, 2); // 3.64

?>

PHP to round up to the 2nd decimal place

You can use 2 functions:

  • round() - docs here: http://www.php.net/manual/en/function.round.php
  • number_format() - docs here: http://ro1.php.net/number_format

I've used both with success, and depending on what you're doing with the result, you may chose either of the above functions.

Later edit: If you want to only round up, you can use ceil() - http://www.php.net/manual/en/function.ceil.php + number format or round

echo round(ceil($number*100)/100,2);

As another user suggested earlier

Two Decimal Places without Rounding (Positive and Negative)

number_format will always round, you can however use below code to make it work:

$number = 0.9999999; 
echo number_format(floor($number*100)/100, 2); //Returns 0.99

Note: use floor() for positive numbers and ceil() for negative numbers.

PHP: Round a number upto 2 decimal places and if the number is a whole number then add trailing zeros

You can try like this:

sprintf("%0.2f",$number);

Check sprintf

or try like this:

$rounded_value = number_format($number,2);

Check number_format

PHP Round up long float to two decimals in accounting

you can use this function

<?php
function round_up ( $value, $precision ) {
$pow = pow ( 10, $precision );
return ( ceil ( $pow * $value ) + ceil ( $pow * $value - ceil ( $pow * $value ) ) ) / $pow;
}
$value = 562.9445;

echo round_up ($value,1)."\n"; // return 563
echo round_up ($value,2)."\n"; // return 562.95
echo round_up ($value,3)."\n"; // return 562.945

if you need to use round function there is some options that dont siuts to you:

<!DOCTYPE html>
<html>
<body>

<?php
$value = 562.9445;
echo(round($value,1,PHP_ROUND_HALF_UP) . "<br>");
echo(round($value ,2,PHP_ROUND_HALF_UP) . "<br>");
echo(round($value ,3,PHP_ROUND_HALF_UP) . "<br>");
echo ("<hr>");

echo(round($value ,1,PHP_ROUND_HALF_DOWN) . "<br>");
echo(round($value ,2,PHP_ROUND_HALF_DOWN) . "<br>");
echo(round($value ,3,PHP_ROUND_HALF_DOWN) . "<br>");
echo ("<hr>");
echo(round($value ,1,PHP_ROUND_HALF_EVEN) . "<br>");
echo(round($value ,2,PHP_ROUND_HALF_EVEN) . "<br>");
echo(round($value ,3,PHP_ROUND_HALF_EVEN) . "<br>");
echo ("<hr>");
echo(round($value ,1,PHP_ROUND_HALF_ODD) . "<br>");
echo(round($value ,2,PHP_ROUND_HALF_ODD) . "<br>");
echo(round($value ,3,PHP_ROUND_HALF_ODD));
?>

</body>
</html>

output:

562.9
562.94
562.945
562.9
562.94
562.944
562.9
562.94
562.944
562.9
562.94
562.945

Rounding down to one decimal

For rounding down in PHP, they have a function called floor(). Unfortunatly, this function does only return an int. However you can make it round down by multiplication first, and then division. See this post: PHP How do I round down to two decimal places?.

This means your code would be something like this:

else if($Input>=1000000){
$AmountCode = "M";
$Amount = floor(floatval($Input / 100000))/10;
}
else if($Input>=1000){
$AmountCode = "K";
$Amount = floor(floatval($Input / 100))/10;

}


Related Topics



Leave a reply



Submit