Eric Lemoine wrote:
> Yes, repr. It was in the provided code snippet. Thanks. Eric

yes, indeed it was. sorry about that.

I think that's a mistake. Python's repr() is designed to satisfy:

eval(repr(object)) == object

that can't always be done, but that's the theory -- it's a good way to 
convert python objects into python code, but in this case, we're turning 
python objects into JavaScript code.

In the case of floats, repr() returns a litteral that has enough digits 
to guarantee the above. So, for instance, when you have start with a 
value that can't be exactly represented as a float, you get a lot of digits:

 >>> f = 0.1
 >>> repr(f)
'0.10000000000000001'

If there is an exact mapping from binary to decimal, you get a cleaner 
result:

 >>> f = 0.128
 >>> repr(f)
'0.128'

Anyway, I doubt that what you really want in JSON is that full number of 
digits.

Options:

(1) Use str() instead. It gives a "clean" result, apparently truncating 
to 12 significant figures. As a python float can hold up to 14 decimal 
figures, that's pretty good.

(2) Use string formatting, with the %g formatter. This lets you specify 
the number of significant figures you want to display:

def FLOAT_REPR(f, Precision=8):
      format_str = "%%.%ig"%Precision
      return format_str%f

I don't know if the geoJSON API has a place to specify precision of 
numbers, but it might be nice.


Either of these would work for floats and decimals (and integers, and 
numpy types, I think)

-Chris





-- 
Christopher Barker, Ph.D.
Oceanographer

Emergency Response Division
NOAA/NOS/OR&R            (206) 526-6959   voice
7600 Sand Point Way NE   (206) 526-6329   fax
Seattle, WA  98115       (206) 526-6317   main reception

[EMAIL PROTECTED]
_______________________________________________
Community mailing list
[email protected]
http://lists.gispython.org/mailman/listinfo/community

Reply via email to