You are here

function gmap_decimal in GMap Module 7

Same name and namespace in other branches
  1. 5 gmap.module \gmap_decimal()
  2. 6.2 gmap.module \gmap_decimal()
  3. 6 gmap.module \gmap_decimal()
  4. 7.2 gmap.module \gmap_decimal()

Utility function to allow high-precision decimals to work with the SQL layer. Use concatenation. (Apparently unquoted %s is bad.)

File

./gmap.module, line 1169
GMap -- Routines to use the Google Maps API in Drupal.

Code

function gmap_decimal($num) {

  // Paraphrased from postgresql documentation:
  //
  // Numbers in SQL can be in one of these forms:
  //   digits
  //   digits.[digits][e[+-]digits]
  //   [digits].digits[e[+-]digits]
  //   digitse[+-]digits
  // where "digits" is one or more decimal digits.
  // Trim extra whitespace
  $num = trim($num);

  // Check if we're in an acceptable form.
  if (preg_match('/^[+\\-]?((\\d+)|(\\d+\\.\\d*)|(\\d*\\.\\d+))(e[+\\-]?\\d+)?$/', $num) === 1) {

    // Good, we can pass that right along.
    return $num;
  }

  // Otherwise, cast to float, possibly losing precision.
  return (double) $num;
}