0

Ultimately, I am trying to accomplish the calculation of a number that gives me back 3 decimal places, and round it to 2 decimal places. What I have, is thus:

total = 175

tax = .0875

total += total * tax  # giving me my desired total
print total
>>> 190.3125

rounded = round(total, 2)  # giving me my desired decimal place
print rounded
>>> 190.31

I was thinking of doing the total and rounded line in one, but it looks jumbled...

Here is what I feel is the fastest way, but does not look as readable as I would like:

total = round((total + (total*tax)), 2)

print total
>>> 190.31

EDIT

Thank you guys for the input in the comments. It seems time is clearly not something to stress here & I am going to run with rounded = round(total, 2) for my code. Thanks again!

PythonReactor
  • 483
  • 4
  • 18
  • 1
    ... what do you mean by "fastest"? Look, if something is equivalent except being, to your eyes, less readable, why would you go with the less readable alternative? – juanpa.arrivillaga Jan 18 '18 at 00:26
  • 1
    "What do y'all think?" I think you'll probably have a million different things to worry about in the nonsensical world of tax calculation than the "fastest" way of getting this value. `rounded = round(total, 2)` is most pythonic IMO. – roganjosh Jan 18 '18 at 00:29
  • 1
    Also note there's no need for the extra parenthesis in your 'one-liner' solution: `round(total + total * tax, 2) == 190.31`. Either way, as other people have said, just use the most readable solution. No need to began optimizing small things like this. – Christian Dean Jan 18 '18 at 00:30
  • I timed it for you. In the first method: `754 ns ± 3.59 ns per loop (mean ± std. dev. of 7 runs, 1000000 loops each)`. Second method: `758 ns ± 19.3 ns per loop (mean ± std. dev. of 7 runs, 1000000 loops each)`. This question is moot in terms of speed. – roganjosh Jan 18 '18 at 00:38
  • I was using "Pythonic" as meaning "fast & readable". But I see with the speed, thanks to @roganjosh, that fast isn't something to question. Thanks! – PythonReactor Jan 18 '18 at 00:43
  • @juanpa.arrivillaga I was hoping to keep it most readable in the case that someone else reads my code, but also wanted to not create unnecessary lines. – PythonReactor Jan 18 '18 at 00:43
  • ... are you suffering from a shortage of lines? Like, honestly, are you in an embedded environment or something? Note, Pythonic does not mean "as little lines as necessary", quite often, it means the *opposite of that*. – juanpa.arrivillaga Jan 18 '18 at 00:52
  • `round()` doesn't actually usually round, it just mushes the number into something closer. – Ignacio Vazquez-Abrams Jan 18 '18 at 00:52
  • I may have just learned something more valuable than what I was searching for, @juanpa.arrivillaga & ignacio, I have noticed it rounds up not at 5, but at 6 – PythonReactor Jan 18 '18 at 00:53
  • @IgnacioVazquez-Abrams what do you mean? – juanpa.arrivillaga Jan 18 '18 at 00:55
  • @juanpa.arrivillaga: Standard FP [BS](https://stackoverflow.com/questions/588004/is-floating-point-math-broken). – Ignacio Vazquez-Abrams Jan 18 '18 at 00:56
  • @M.Gilbert no, it just uses what has become the preferred method of rounding, exact halfway cases rounded to the nearest even result instead of away from zero. round(2.5) -> 2 rather than 3 [Read more here](https://stackoverflow.com/questions/10825926/python-3-x-rounding-behavior). I believe the term sometimes used is "the banker's round". EDIT: In Python 3, Python 2 keeps the "usual" method I believe, as a default. – juanpa.arrivillaga Jan 18 '18 at 00:59

1 Answers1

0

I think you should first think pythonically. What is the critical purpose of rounding a float? Do you need to use this rounded float to calculate later or you want to display it? Or you want to store it into a file or database with a consistent format? Different purposes lead to different methods.

If you want to use this rounded float to calculate later, you just don't need to round it. We should not do any additional process before the final calculation as it will only reduce the precision of results without any benefit.

If you want to display this rounded float, you can keep the precision inside variable and use string format: print "%.2f" % total. This will also print out what you want but won't make you lose precision.

If you want to store it into a file or a database, you can just go to rounded = round(total, 2), it is really pythonic and useful.

Sraw
  • 18,892
  • 11
  • 54
  • 87