Query doesn't seem to like number_format

9 views
Skip to first unread message

sdotsen

unread,
Dec 13, 2009, 6:25:20 PM12/13/09
to mongodb-user
$min_lat = number_format($zip_lat - $lat_range, "4", ".", "");
$max_lat = number_format($zip_lat + $lat_range, "4", ".", "");
$min_lon = number_format($zip_lon - $lon_range, "4", ".", "");
$max_lon = number_format($zip_lon + $lon_range, "4", ".", "");

I printed each variable and it produces the following:

$min_lat 33.8038
$max_lat 34.0930
$min_lon -81.5601
$max_lon -81.2075

The following php query using mongo php's driver doesn't seem to like
the variables:

$query = array( "lat" => array('$gt' => $min_lat, '$lt' => $max_lat ),
"lon" => array( '$gt' => $min_lon, '$lt' => $max_lon ));

Now, if I hardcode the numbers into the query or if assign the
variables w/ just the number itself, it works.

This works:

$min_lat = 33.8038
$max_lat = 34.0930

John Wyles

unread,
Dec 13, 2009, 9:15:24 PM12/13/09
to mongod...@googlegroups.com
Just a thought; have you tried casting to float?

$min_lat = (float) number_format($zip_lat - $lat_range, "4", ".", "");
$max_lat = (float) number_format($zip_lat + $lat_range, "4", ".", "");
$min_lon = (float) number_format($zip_lon - $lon_range, "4", ".", "");
$max_lon = (float) number_format($zip_lon + $lon_range, "4", ".", "");

-John
> --
>
> You received this message because you are subscribed to the Google Groups "mongodb-user" group.
> To post to this group, send email to mongod...@googlegroups.com.
> To unsubscribe from this group, send email to mongodb-user...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/mongodb-user?hl=en.
>
>
>

sdotsen

unread,
Dec 13, 2009, 9:29:03 PM12/13/09
to mongodb-user
that did it! thx ... why did I have to convert to float? It wasn't a
string or anything.

On Dec 13, 9:15 pm, John Wyles <jcwy...@gmail.com> wrote:
> Just a thought; have you tried casting to float?
>
> $min_lat = (float) number_format($zip_lat - $lat_range, "4", ".", "");
> $max_lat = (float) number_format($zip_lat + $lat_range, "4", ".", "");
> $min_lon = (float) number_format($zip_lon - $lon_range, "4", ".", "");
> $max_lon = (float) number_format($zip_lon + $lon_range, "4", ".", "");
>
> -John
>

John Wyles

unread,
Dec 13, 2009, 10:08:59 PM12/13/09
to mongod...@googlegroups.com
var_dump(gettype(number_format(12345 - 43.5334492313, "4", ".", "")));
string(6) "string"


From PHP.net (http://php.net/number_format):
string number_format ( float $number , int $decimals , string
$dec_point , string $thousands_sep )

-John
Reply all
Reply to author
Forward
0 new messages