Round number down to the next 1000 in python [duplicate] - python

This question already has answers here:
Python: how can I round a number downward to next 1000
(4 answers)
Closed 10 months ago.
can somebody tell me how i can round down to the nearest thousand. So far I tried it with math.round(), the truncate function but i couldn't find my math skills to work out for me. As a example for some people I want that 4520 ends up in beeing 4000.

In Python, you can do
print((number // 1000)*1000)

just a thought
Why not do it the traditional way?!
deprecate_vals = 3
val = 4520
val = int(val/(10**deprecate_vals)) * (10**deprecate_vals)
print(val)

You can divide by 1000 before rounding and then multiply by 1000.
invalue = 4320
outvalue = 1000 * round(invalue/1000)
print("invalue: ",invalue)
print("rounded value: ",outvalue)
output
invalue: 4320
rounded value: 4000

There is multiple way to do it.
You can get the digit for the thousand by using the division (// mean without remaining, so you don't get 4,52 but 4)
x = 4520
rounded_x = (x//1000) * 1000
You can also use the remaining of the division with the modulo operator and remove it to the value :
x = 4520
rounded_x = x - (x%1000)

Related

Python: is there a way to 'cleanly' divide two numbers of type float and int? [duplicate]

This question already has answers here:
Why does integer division yield a float instead of another integer?
(4 answers)
Closed 3 years ago.
Pretty new to python, facing a problem that requires basically the opposite of the remainder "%" function. For example, if I wanted to divide 81.5 by 20, my output would be 4. My best attempt is as follows:
amount = 81.504
round(amount, 2)
num20s = amount / 20
int(num20s)
I've tried several different combinations of the above code, but nothing has worked so far. This is the closest I've gotten to what I want, but it won't work in edge cases, and for some reason still represents the number with a ".0" at the end, so that last line must not be doing anything.
Integer division operator in python is "//".
>>> amount = 81.504
>>> amount // 20
Out[3]: 4.0
>>> int(amount // 20)
Out[4]: 4

Terminal not printing python code on Mac [duplicate]

This question already has answers here:
How can I force division to be floating point? Division keeps rounding down to 0?
(11 answers)
Closed 5 years ago.
I made a small python file on my desktop for fun, and I'm frustrated to find that no values are being returned. Here is the code from the file:
def calc(x):
chance =1
for i in range(1,x+1):
chance *= ((365-i)/365)
return chance * 100
string = calc(23)
print string
And here is what I type into the terminal:
cd Desktop #where file is located
python py.py #py is the name of the file
And this is what it returns:
0
Since 365 and i are integers, you are performing integer division, which only returns the "whole" part of the result. Since 365-i is lesser than 365 dividing the two will result in 0, and continuously multiplying it by anything will still give 0.
You could use the 365.0 notation to use floating point division, which will eventually generate the result 46.1655742085:
chance = 1
for i in range(1,x+1):
chance *= ((365.0 - i) / 365.0)
# Floating point^-----------^
Well so on your first iteration your i = 1. 365-1 = 364. 364/365 = 0 because it is integer division in python. So the answer will always give you 0 unless you change it to allow for you to give a float.
Changing 365 to 365.0 will give you your answer
chance = chance * ((365-i)/365.0)
I just changed the 365 to 365.0 and I got
2.23071139399e-05
As my output
The result is correct in Python2.x. When a/b and a is less than b, result is 0.
Modify 365 to 365.0 it will cast the division result to float
chance *= ((365-i)/365.0)

Tip calculator returns zeros [duplicate]

This question already has answers here:
What is the difference between '/' and '//' when used for division?
(16 answers)
Closed 6 years ago.
I'm having some trouble running the code I wrote.
meal = float(raw_input("How much did your meal cost? "))
tax = 6.75 / 100
tip = 15 / 100
total = (meal * tax) * tip
print total
You can see above that I made this "tip calculator". Whenever I enter a number, it returns with zeros. It seems like it's skipping the entire calculation part.
Any solution?
If it is python 2.7 you are doing integer division, which means your tip is calculating as 15/100 = 0.
from __future__ import division
At the the top of the program will solve this.
In python 2, / is integer division. Thus, 15 / 100 = 0, and you miltiply by that. Just replace 15 with 15.0
The problem is on this line:
tip = 15 / 100
Dividing an int by an int will result in an int. In this case 15 / 100 gives you 0, and the complementary modulo operation 15 % 100 gives the 15 remainder.
If you change 15 to a float it will work because precision is preserved and tip will be a float instead of an int. Try:
tip = 15.0 / 100

Python sum of digits error [duplicate]

This question already has answers here:
Why does integer division yield a float instead of another integer?
(4 answers)
Closed 8 years ago.
While solving Project Euler problems with Python (which I am a beginner at), I got this following error. The question is to find the sum of the digits of 2^1000. For that I wrote the following code :
sum=0
x=2**1000
while(x):
sum += x%10
print(sum) #Just to check whats happening
x /= 10
print("\n"*5)
print("Sum = ",sum)
For this, I get decimal added up somewhere in between.
Output :
6
10.0
10.0
12.0
16.0
....
1116.0
1122.0
1131.625 #Why does the decimal get added?
1138.59375
.....
1181.495136589947
1186.5812084526442
1188.089815638914
1195.240676357541
1195.9557624294036
1197.0272710365898
1197.1344218973084
1197.1451369833803
1197.1462084919874
.....
1197.1463275484991 #This number gets repeated a lot of times
1197.1463275484991
1197.1463275484991
Sum = 1197.1463275484991
Please explain what's going on and help.
Use integer division instead of floating point:
x //= 10
Do not know if you're looking for an alternative implementation, but this might be more straightforward if you don't want to risk crossing over into floating point land.
# Python 2.7
x = str(2**1000)
print sum([int(i) for i in x])

Division error in python, result is incorrectly rounded [duplicate]

This question already has answers here:
How can I force division to be floating point? Division keeps rounding down to 0?
(11 answers)
Closed 8 years ago.
I am trying to make a BMI calculator function. I am learning python at pyschools.
This is my code:
# Note: Return a string of 1 decimal place.
def BMI(weight, height):
x = weight /(height*height)
g = round(x,1)
return g
And pyschools shows me that these are the right answers:
With 110 = weight and 2 = height I am supposed to get a BMI of 27,5.
But I instead get 27.
Then it does a second check to make sure I wrote the code right and tells me 24,2 is the right answer but my program did return 24,2. But it still marks my answer in red and says "my" 24,2 is wrong and the website's is right.
If someone has a better site or anything to learn python it would also be appreciated since this website seems to be kind of wrong sometimes. And I am looking for free online resources. No books please.
To fix it for all cases, add this line to the top:
from __future__ import division # Make division work like in Python 3.
in Python 2, / means integer division.
With this in mind, in Python 2 if you pass intgers into division, it will give you an integer back. Anything that would have been a float is floored*. Therefore another option to get the desired result is to pass a float in, so instead of:
weight / (height*height)
do:
float(weight) / (height*height) # float in means float out.
*This means that only full times that the divisor goes in are counted. So 1/2 will get 0 because 2 goes fully into 1 0 times.
def BMI(weight, height):
x = float(weight) /(height*height)
g = round(x,1)
return g
see Python division
and Binary arithmetic operations
The issue lies with your division.
Division as we intrinsically know it is floating point division, or division where 1 / 2 evaluates to a fraction, 0.5. In standard programatic division, the 1, 2 are ints() and therefore cant be fractions, or floats() as the type is called in python. The expression, 1 / 2 therefore evaluates as 0, as 2 as a whole integer cant go into one entirely any times.
Ex:
In [1]: 1 / 2
Out[1]: 0
# Explicitly what is going on, since 1 and 2 are ints.
In [2]: int(1) / int(2)
Out[2]: 0
#fixed with floating division
In [3]: float(1) / float(2)
Out[3]: 0.5
# protip: only one of the divisors needs to be a float for python to divide correctly.
In [4]: 1 / float(2)
Out[4]: 0.5
Use x = weight / float((height*height)) to get the results you expect.
# Note: Return a string of 1 decimal place.
def BMI(weight, height):
x = weight / float((height*height))
g = round(x,1)
return g

Categories

Resources