## Round up and ignore the digits after the decimal

guyute74
Posts: 17
Joined: Thu Feb 07, 2013 5:05 pm

### Round up and ignore the digits after the decimal

I am running a python script to read temperatures and am getting temperatures up to ten thousandths accurate on my print function.

What is the best way to eliminate this?

while lines[0].strip()[-3:] != 'YES':
time.sleep(0.2)
equals_pos = lines[1].find('t=')
if equals_pos != -1:
temp_string = lines[1][equals_pos+2:]
temp_C = float(temp_string) / 1000.0
temp_F = temp_C * 9.0 / 5.0 + 32.0
return temp_F

mefarmer
Posts: 36
Joined: Wed Mar 13, 2013 10:53 pm

### Re: Round up and ignore the digits after the decimal

I love Python, but this is one of those task that seems like it should be simple but isn't.
You have to use string formatting....
Here is an example from the interpreter:

Code: Select all

``````>>> long_float = 1.234123493536346541275341234
>>> # ok this will round to the 4th digit after the decimal
>>> round(long_float,4)
1.2341
>>> # ok that seems like what you want huh?
>>> round(long_float,6)
1.2341230000000001
>>> # yea here's the problem floats are not exact
>>> # this has to do with the way numbers are stored in binary
>>> # it will vary across processors.... Blah!
>>> "%.6f"%(round(long_float,6))
'1.234123'
>>> # so we can use string formating to save the day, but it is a str.
>>> # lets convert this back to a float
>>> float("%.6f"%(round(long_float,6)))
1.2341230000000001
>>> # arghhhhhh!
>>> # lesson of the day if you just need a nice string repr
>>> # of a float it's easy enough, but floats don't always play nice``````
If you want to just truncate to a specific digit just use string format.
If you care about rounding round first and then string format.

guyute74
Posts: 17
Joined: Thu Feb 07, 2013 5:05 pm

### Re: Round up and ignore the digits after the decimal

Outstanding! I will start monkeying with this soon.
I admit, I was hoping for an easier answer but as you say... I guess this is what I will learn about Python.

mefarmer
Posts: 36
Joined: Wed Mar 13, 2013 10:53 pm

### Re: Round up and ignore the digits after the decimal

I forgot to mention that this is not a Python specific problem with floats.
There is also a "decimal" module that I always seem to forget about.
From the module:
""" This is a Py2.3 implementation of decimal floating point arithmetic based on
the General Decimal Arithmetic Specification:

http://speleotrove.com/decimal/decarith.html

and IEEE standard 854-1987:

http://www.cs.berkeley.edu/~ejr/project ... 7/dir.html

Decimal floating point has finite precision with arbitrarily large bounds.

The purpose of this module is to support arithmetic using familiar
"schoolhouse" rules and to avoid some of the tricky representation
issues associated with binary floating point. The package is especially
useful for financial applications or for contexts where users have
expectations that are at odds with binary floating point (for instance,
in binary floating point, 1.00 % 0.1 gives 0.09999999999999995 instead
of the expected Decimal('0.00') returned by decimal floating point)."""

It is in the python standard library.

jojopi
Posts: 3043
Joined: Tue Oct 11, 2011 8:38 pm

### Re: Round up and ignore the digits after the decimal

mefarmer wrote:>>> "%.6f"%(round(long_float,6))
'1.234123'
You just want one of:

Code: Select all

``````>>> "%.6f" % long_float
'1.234123'
>>> "{:.6f}".format(long_float)
'1.234123'``````
It is not usually desirable to round intermediate results. Keep everything at its highest precision except when you print it.

(If you need to represent exact things like currency then avoid fractions by denominating your variables in cents or hundredths of cents. The problem converting to and from binary only affects digits to the right of the decimal point. It is because the decimal fraction 0.1 is infinite recurring bicimal 0.0001100110011…)

mefarmer
Posts: 36
Joined: Wed Mar 13, 2013 10:53 pm

### Re: Round up and ignore the digits after the decimal

It is not usually desirable to round intermediate results. Keep everything at its highest precision except when you print it.
I am running a python script to read temperatures and am getting temperatures up to ten thousandths accurate on my print function.
What is the best way to eliminate this?
I showed him two ways to do it. One just truncating the results and the other rounding to your lowest desired digit and then truncating.
Good example of Python 3 format I still use Python 2.x on all my work.

mefarmer
Posts: 36
Joined: Wed Mar 13, 2013 10:53 pm

### Re: Round up and ignore the digits after the decimal

I just read your post title again. Do you want to ignore all the digits after the decimal? If so just round and then convert to an int.

Code: Select all

``````>>>temp = 98.523443244234
>>>print int(round(temp))
99
``````

jojopi
Posts: 3043
Joined: Tue Oct 11, 2011 8:38 pm

### Re: Round up and ignore the digits after the decimal

mefarmer wrote:I showed him two ways to do it. One just truncating the results and the other rounding to your lowest desired digit and then truncating.
All of your methods round, and so do mine. There is no need to round twice.

Code: Select all

``````>>> "%.3f" % 1.5555555555555555
'1.556'
>>> "%.0f" % 1.5555555555555555
'2'``````

mefarmer
Posts: 36
Joined: Wed Mar 13, 2013 10:53 pm

### Re: Round up and ignore the digits after the decimal

You are right and I must ask for forgiveness now
I had forgotten that it rounded on it's own.
Thank you for the correction.

pyedish
Posts: 1
Joined: Sat Jan 20, 2018 3:01 pm

### Re: Round up and ignore the digits after the decimal

I came across this topic when searching for a solution to the same problem; handling the output from a Dallas temperature probe. The solutions offered seemed rather complex for me as someone new to Python although I have some programming experience.

The output of the probe is a six digit string e.g. 123456 representing a temperature of 12.3456 degrees Celsius. Dividing this number by 10000 gives 12.3456 which is a greater degree of resolution than most people require. If we divide the output by 1000 and round the result we get 123; dividing this by 10 gives 12.3 which is an adequate resolution for most purposes. This can be achieved by using the expression

round ( temp /1000) / 10