0

This is a follow-up question to this and I looked at this one as well. My Python script collects data during every run and sends data to a URL. The URL should display data/values up to 2 decimal places. I modified my script to round off the values to 2 decimal places as shown below. After doing so, any and all print statements show that my script is rounding off to 2 decimal places. Even after urlencode, when I decode the params, it shows 2 decimal places. But at the URL, the values show up with all the decimal places - instead of 1.0, say, it shows 1.000342760436734 whereas the values should show as 1.00 and not more.

Here's the code in my script with the print outputs:

//The data that is collected by the script - a list of dicts.

y = [{'a': 80.0, 'b': 0.0786235, 'c': 10.0, 'd': 10.6742903}, {'a': 80.73246, 'b': 0.0, 'c':   
10.780323, 'd': 10.0}, {'a': 80.7239, 'b': 0.7823640, 'c': 10.0, 'd': 10.0}, {'a': 
80.7802313217234, 'b': 0.0, 'c': 10.0, 'd': 10.9762304}]

//The code that rounds off the decimal places to 2

class LessPrecise(float):
    def __repr__(self):
        return str(self)

def roundingVals_toTwoDeci(y):
    for d in y:
        for k, v in d.iteritems():
            v = LessPrecise(round(v, 2))
            print v
            d[k] = v

//Json.dumps

roundingVals_toTwoDeci(y)
j = json.dumps(y)
print j

//print j gives

[{"a": 80.0, "b": 0.0, "c": 10.0, "d": 10.0}, {"a": 100.0, "b": 0.0, "c": 0.0, "d": 0.0}, {"a":  
80.0, "b": 0.0, "c": 10.0, "d": 10.0}, {"a": 90.0, "b": 0.0, "c": 0.0, "d": 10.0}] 

//urlencoding it

params = urllib.urlencode({'thekey': j}) 

//after decoding params, I get

thekey=[{"a": 80.0, "b": 0.0, "c": 10.0, "d": 10.0}, {"a": 100.0, "b": 0.0, "c": 0.0, "d": 
0.0}, {"a": 80.0, "b": 0.0, "c": 10.0, "d": 10.0}, {"a": 90.0, "b": 0.0, "c": 0.0, "d": 10.0}]

//So far, so good. At the URL, however, instead of 10.0, it shows 10.000436783313897. I don't what's going on or how to fix it.

I should mention that I get the same float values at the URL even after I convert values into strings directly as in:

def roundingVals_toTwoDeci(y):
    for d in y:
        for k, v in d.iteritems():
            d[k] = str(round(v, 2))

        return

With this, print s=json.dumps() gives values like {"a": "10.0", "b": "3.1", etc.} but at the URL, values are 10.856473985798743.

Community
  • 1
  • 1
askance
  • 1,077
  • 4
  • 14
  • 19
  • how can we reproduce it? – Foo Bar User Oct 03 '13 at 18:21
  • The URL in question is from an internal app/server. I can include the entire script but I don't know how you'll reproduce the URL values. I don't know if I can include a screenshot here of the URL but it would just show what I have already mentioned - values of 15 decimal places - e.g., 10.123456789012345 – askance Oct 03 '13 at 18:32

0 Answers0