Skip to content

Instantly share code, notes, and snippets.

@Alir3z4
Created February 11, 2019 15:46
Show Gist options
  • Save Alir3z4/705a03413d81ad1f45980db65725e132 to your computer and use it in GitHub Desktop.
Save Alir3z4/705a03413d81ad1f45980db65725e132 to your computer and use it in GitHub Desktop.
Will format the given number by $precision and separated by $separator.
<?php
/**
* numberFormatPrecision
*
* Will format the given number by $precision and separated by $separator.
*
* @param string|int|float $number
* @param int $precision
* @param string $separator
* @return string
*/
function numberFormatPrecision($number, $precision = 2, $separator = '.')
{
$numberParts = explode($separator, $number);
$response = $numberParts[0];
if (count($numberParts) > 1) {
$response .= $separator;
$response .= substr($numberParts[1], 0, $precision);
}
return $response;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment