Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 7 additions & 4 deletions googlemaps/convert.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,10 @@
def format_float(arg):
"""Formats a float value to be as short as possible.

Trims extraneous trailing zeros and period to give API
args the best possible chance of fitting within 2000 char
URL length restrictions.
Truncates float to 8 decimal places and trims extraneous
trailing zeros and period to give API args the best
possible chance of fitting within 2000 char URL length
restrictions.

For example:

Expand All @@ -45,13 +46,15 @@ def format_float(arg):
format_float(40.1) -> "40.1"
format_float(40.001) -> "40.001"
format_float(40.0010) -> "40.001"
format_float(40.000000001) -> "40"
format_float(40.000000009) -> "40.00000001"

:param arg: The lat or lng float.
:type arg: float

:rtype: string
"""
return ("%f" % float(arg)).rstrip("0").rstrip(".")
return ("%.8f" % float(arg)).rstrip("0").rstrip(".")


def latlng(arg):
Expand Down
16 changes: 16 additions & 0 deletions googlemaps/test/test_convert.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

import datetime
import unittest
import pytest

from googlemaps import convert

Expand Down Expand Up @@ -152,3 +153,18 @@ def test_polyline_round_trip(self):
points = convert.decode_polyline(test_polyline)
actual_polyline = convert.encode_polyline(points)
self.assertEqual(test_polyline, actual_polyline)


@pytest.mark.parametrize(
"value, expected",
[
(40, "40"),
(40.0, "40"),
(40.1, "40.1"),
(40.00000001, "40.00000001"),
(40.000000009, "40.00000001"),
(40.000000001, "40"),
],
)
def test_format_float(value, expected):
assert convert.format_float(value) == expected
4 changes: 2 additions & 2 deletions googlemaps/test/test_distance_matrix.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,8 @@ def test_mixed_params(self):
self.assertEqual(1, len(responses.calls))
self.assertURLEqual('https://maps.googleapis.com/maps/api/distancematrix/json?'
'key=%s&origins=Bobcaygeon+ON%%7C41.43206%%2C-81.38992&'
'destinations=43.012486%%2C-83.696415%%7C42.886386%%2C'
'-78.878163' % self.key,
'destinations=43.012486%%2C-83.6964149%%7C42.8863855%%2C'
'-78.8781627' % self.key,
responses.calls[0].request.url)

@responses.activate
Expand Down
2 changes: 1 addition & 1 deletion googlemaps/test/test_geocoding.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ def test_reverse_geocode(self):

self.assertEqual(1, len(responses.calls))
self.assertURLEqual('https://maps.googleapis.com/maps/api/geocode/json?'
'latlng=-33.867487%%2C151.20699&key=%s' % self.key,
'latlng=-33.8674869,151.2069902&key=%s' % self.key,
responses.calls[0].request.url)

@responses.activate
Expand Down