Advertisement
donnykurnia

number format helpers

Aug 13th, 2016
213
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 0.99 KB | None | 0 0
  1. <?php
  2.  
  3. if ( !function_exists('floatFilter') ) {
  4.   // filter any character beside number, dot, comma, and scientific e
  5.   function floatFilter($number) {
  6.     return filter_var($number,
  7.       FILTER_SANITIZE_NUMBER_FLOAT,
  8.       FILTER_FLAG_ALLOW_THOUSAND | FILTER_FLAG_ALLOW_FRACTION | FILTER_FLAG_ALLOW_SCIENTIFIC);
  9.   }
  10. }
  11.  
  12. if ( !function_exists('idToFloat') ) {
  13.   // This function convert Indonesian number to English number
  14.   // 1.234.567,89 to 1234567.89
  15.   function idToFloat($numberWithComma='') {
  16.     return floatval(
  17.       str_replace( ',', '.',
  18.         str_replace( '.', '', floatFilter($numberWithComma) ) ) );
  19.   }
  20. }
  21.  
  22. if ( !function_exists('floatToId') ) {
  23.   // This function convert number to Indonesian formatted number
  24.   function floatToId($numberWithDot='', $precision=2, $trimZero=false) {
  25.     $result = number_format( $numberWithDot, $precision, ',', '.' );
  26.     if ( $trimZero ) {
  27.       $result = rtrim( rtrim( $result, '0' ), ',' );
  28.     }
  29.     return $result;
  30.   }
  31. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement