donnykurnia icon

number format helpers

donnykurnia | PRO | 08/14/16 04:31:46 AM UTC | 0 ⭐ | 373 👁️ | Never ⏰ | []
PHP |

990 B

|

None

|

0 👍

/

0 👎

<?php
 
if ( !function_exists('floatFilter') ) {
  // filter any character beside number, dot, comma, and scientific e
  function floatFilter($number) {
    return filter_var($number,
      FILTER_SANITIZE_NUMBER_FLOAT,
      FILTER_FLAG_ALLOW_THOUSAND | FILTER_FLAG_ALLOW_FRACTION | FILTER_FLAG_ALLOW_SCIENTIFIC);
  }
}
 
if ( !function_exists('idToFloat') ) {
  // This function convert Indonesian number to English number
  // 1.234.567,89 to 1234567.89
  function idToFloat($numberWithComma='') {
    return floatval(
      str_replace( ',', '.',
        str_replace( '.', '', floatFilter($numberWithComma) ) ) );
  }
}
 
if ( !function_exists('floatToId') ) {
  // This function convert number to Indonesian formatted number
  function floatToId($numberWithDot='', $precision=2, $trimZero=false) {
    $result = number_format( $numberWithDot, $precision, ',', '.' );
    if ( $trimZero ) {
      $result = rtrim( rtrim( $result, '0' ), ',' );
    }
    return $result;
  }
}

Comments