You are here

function webform_number_format in Webform 7.4

Same name and namespace in other branches
  1. 6.3 components/number.inc \webform_number_format()
  2. 7.3 components/number.inc \webform_number_format()

Format a number with thousands separator, decimal point, and decimal places.

This function is a wrapper around PHP's native number_format(), but allows the decimal places parameter to be NULL or an empty string, resulting in a behavior of no change to the decimal places.

2 calls to webform_number_format()
_webform_number_format in components/number.inc
Apply number format based on a component and number value.
_webform_validate_number in components/number.inc
A Drupal Form API Validation function.

File

components/number.inc, line 859
Webform module number component.

Code

function webform_number_format($value, $decimals = NULL, $point = '.', $separator = ',') {
  if (!is_numeric($value)) {
    return '';
  }

  // If no decimal places are specified, do a best guess length of decimals.
  if (is_null($decimals) || $decimals === '') {

    // If it's an integer, no decimals needed.
    if (filter_var((double) $value, FILTER_VALIDATE_INT) !== FALSE) {
      $decimals = 0;
    }
    else {
      $decimals = strlen($value) - strrpos($value, '.') - 1;
    }
    if ($decimals > 4) {
      $decimals = 4;
    }
  }
  return number_format($value, $decimals, $point, $separator);
}