PHP Decimal Place Handling Print

  • 14

The goal here is to show the different method for dealing with decimal places on a floating number.

<?php

// Example
  $float = '100.12345';

// Round
  echo round($float, 2);

// Floor with floor()
  echo floor($float * 100) * 0.01;

// Floor by INT casting
  echo (int)($float * 100) * 0.01;

// Ceiling with ceil()
  echo ceil($float * 100) * 0.01;

// Trim using sprintf()
  echo sprintf("%.2f", $float);

// Trim using number_format()
  echo number_format($float, 2, '', '.');

?>

Was this answer helpful?

« Back