You are here

function webform_number_format in Webform 6.3

Same name and namespace in other branches
  1. 7.4 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. Validates the entered values from number components on the client-side form.

File

components/number.inc, line 784
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 (is_int(($value . '') * 1)) {
      $decimals = 0;
    }
    else {
      $decimals = strlen($value) - strrpos($value, '.') - 1;
    }
    if ($decimals > 4) {
      $decimals = 4;
    }
  }
  return number_format($value, $decimals, $point, $separator);
}