Division in python. 7/9 = 0? How to stop this? [duplicate] - python

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
how can I force division to be floating point in Python?
I'm very sorry if this question has been asked already.
timothy_lewis_three_pointers_attempted = 4
timothy_lewis_three_pointers_made = 2
print 'three pointers attempted: ' + str(timothy_lewis_three_pointers_attempted)
print 'three pointers made: ' + str(timothy_lewis_three_pointers_made)
print 'three point percentage: ' + str(timothy_lewis_three_point_percentage)
I'm getting 0 for the percentage. How do I get it to say .5? I know that if I type the numbers as 4.0 and 2.0, I'll get the desired result, but is there another way of doing it?

The other option you have (although I don't recommend it) is to use
from __future__ import division
and then
>>> 7 / 9
0.7777777777777778
This is based on PEP 238.

Make one of them a float:
float(timothy_lewis_three_pointers_made) / timothy_lewis_three_pointers_attempted

You are doing integer division. Make at least one of them a float value
percentage = float(_made) / float(_attempted)
You can also get nicer looking output for percentages by using the new string format method.
"Three point percentage: {:.2%}".format(7.0/9)
# OUT: ' Three point percentage: 77.78%'

Related

When Rounding to nearest hundreds, how do I include 0s [duplicate]

This question already has answers here:
Rounding a number in Python but keeping ending zeros
(6 answers)
Closed 6 years ago.
So let's say I have this code:
num = 1.29283
round(num, 2)
That rounds to 1.29, but if I do this:
num = 1.30293
round(num, 2)
That rounds to 1.3. I want to know if there is a way to have it round to 1.30; I know it is the same number, but I need it to print 1.30.
You can use string formatting for this. A number in python does not have such a thing as trailing zeros. So your question only make sense for strings.
Example:
>>> num = 1.30293
>>> "{:.2f}".format(num)
'1.30'
The .2f says that this is a float (f) and that you want two digits after the point .2. Read more about string formatting here

How to limit decimal places when using complex numbers [duplicate]

This question already has answers here:
Formatting Complex Numbers
(6 answers)
Closed 8 years ago.
sqrtnum_ = 0
sqrtnum_ = cmath.sqrt(snum_)
print "Using cmath.sqrt:"
print " sqrt(", snum_, ") = ", '%.12f' %sqrtnum_
How can I limit the number of decimal places. Normally I would use the above but it does not work with complex numbers
You can use imag and real attributes of a complex number to get their respective values.
>>> a = cmath.sqrt(-1000)
>>> "%.12f + %.12fj"%(a.real,a.imag)
'0.000000000000 + 31.622776601684j'
This works for completely real numbers also
>>> a = cmath.sqrt(1000)
>>> "%.12f + %.12fj"%(a.real,a.imag)
'31.622776601684 + 0.000000000000j'
This looks to be a duplicate of Formatting Complex Numbers
hopefully that helps!
(would have followed duplicate marking/commenting/etc protocol, but lack the rep for it)

How can I stop printing a float 3 spaces after decimal? [duplicate]

This question already has answers here:
How to display a float with two decimal places?
(13 answers)
Pad python floats
(4 answers)
Closed 8 years ago.
I'm sorry, I know this must be a duplicate, I can't find where else it's posted. Please feel free to link me to the original question and mark this as duplicate.
I would like to print a 3 digits of a number AFTER the decimal point in it.
For example:
number = 523.637382
I would like to print: 523.637
I have a feeling I can use something similar to this
print(str(number)[:7])
>>>523.637
However, this will not work if the number before the decimal is not 3 decimals.
Bonus points:
Would this be easy?
number = 500.220
#magic
>>>500.22
number = 500.2000003
#magic
>>>500.2
A (built-in) function that could do this is round:
>>> number = 523.637382
>>> rounded = round(number, 3) # 3 decimal places, for example
>>> rounded
523.637
This has already been answered for example here.
The good news, to answer the second part of your question, is that the round function automatically removes trailing zeroes. It's much harder to retain the zeros if you're defining a new variable: you need the decimal module; but it looks that that isn't necessary here.
>>> number = 523.60000001
>>> rounded = round(number, 3)
>>> rounded
523.6
print("%.3f" % number)
or, using the new-style formatting,
print("{0:.3f}".format(number))
If you're printing a str like above you can use string interpolation:
number = 33.33333
print("{0:.3f}".format(number))
#=> 33.333

Remove decimal places to certain digits without rounding [duplicate]

This question already has answers here:
How to truncate float values?
(31 answers)
Closed 9 years ago.
I've found dozen of answers, but non of them is what I'm looking for, I don't want to round up or down, I know that I can round numbers as follow:
>>> print('%.3f' % 15.555555)
15.556
>>> round(15.555555, 3)
15.666
But I need to get 15.555. Should I use regex?
Cheeky solution:
numstring = str(15.555555)
num = float(numstring[:numstring.find('.')+4])
My solution involving int abuse. int rounds towards the nearest 0. I multiply it by 10**3 to affect rounding. After using int, I divide it by 10**3 to get actual results.
It's safer, as it does work with e notation.
int(15.55555 * 10**3) / 10.0**3

python round leaving a trailing 0 [duplicate]

This question already has answers here:
Most Pythonic way to print *at most* some number of decimal places [duplicate]
(3 answers)
Closed 9 years ago.
I am trying to round a floating point number in python to zero decimal places.
However, the round method is leaving a trailing 0 every time.
value = 10.01
rounded_value = round(value)
print rounded_value
results in 10.0 but I want 10
How can this be achieved? Converting to an int?
Pass the rounded value to int() to get rid of decimal digits:
>>> value = 10.01
>>> int(round(value))
10
>>> value = 10.55
>>> int(round(value))
11
10.0 and 10 are the same float value. When you print that value, you get the string 10.0, because that's the default string representation of the value. (The same string you get by calling str(10.0).)
If you want a non-default representation, you need to ask for it explicitly. For example, using the format function:
print format(rounded_value, '.0f')
Or, using the other formatting methods:
print '{:.0f}'.format(rounded_value)
print '%.0f' % (rounded_value,)
The full details for why you want '.0f' are described in the Format Specification Mini-Language, but intuitively: the f means you want fixed-point format (like 10.0 instead of, say, 1.0E2), and the .0 means you want no digits after the decimal point (like 10 instead of 10.0).
Meanwhile, if the only reason you rounded the value was for formatting… never do that. Leave the precision on the float, then trim it down in the formatting:
print format(value, '.0f')
Casting to an int would certainly be the easiest way. If you are hell-bent on keeping it a float here's how to do it courtesy of Alex Martelli:
print ('%f' % value).rstrip('0').rstrip('.')
You'll find a function number_shaver() that cuts trailing zeros of numbers in the EDIT 2 of this post.
Another post in the same thread explains how the regex in number_shaver() works.
I improved the regex in another thread some days later.

Categories