This question already has answers here:
Can't convert string number value to float
(3 answers)
Closed 7 years ago.
I seem to have a problem with my code for a simple calculator with equations already codded into it that rounds to decimal points. The problem i have is that I have a variable that has a number from an equation but it isn't rounding to the nearest 2 decimal points when it needs to. Here is an example code:
def main():
variable1 = input("Input number")
variable2 = input("Input number")
V1 = float(variable1)
V2 = float(variable2)
variablesq = V1*V1
equation = 20242*(V2/variablesq)
answer = equation
round(answer, 2) #This is where the problem is occurring
print Answer
I do believe my mistake is that I'm using a variable instead of a set number but say the answer is 15.2353 it won't round the number from the variable to 15.24
Just calling the function won't do anything. You need to assign the result to something.
answer = round(answer, 2)
You can simply do "{0:.2f}".format(answer) that way you don't need to assign it to a variable.
Related
This question already has answers here:
Is floating point math broken?
(31 answers)
Closed 2 years ago.
precision = 2
number = 31684.28
result = Decimal(number) - Decimal(10 ** -precision)
print(result)
Desired output:
31684.27
Actual output:
31684.26999999999883584657356
What I try to do is to subtract 0.01 from number.
You have to make the values with Decimal(...) not the output. So try this:
from decimal import Decimal
precision = 2
number = 31684.28
result = number - float(10 ** Decimal(-precision))
print(result)
Output:
31684.27
You should use formating like the following:
print("{:.2f}".format(result))
Or using round like:
print(round(result, 2))
comment
The question wasn't clear from the start. The correct answer (in my opinion is of #U11-Forward)
You can use the function round
The syntax is: round(number, digits)
So, result = round(number, 2)
You can read more about it here: https://www.w3schools.com/python/ref_func_round.asp
You can use quantize method of decimal library
from decimal import Decimal
result = Decimal('31684.26999999999883584657356').quantize(Decimal('0.01'))
# result = Decimal('31684.27')
This question already has answers here:
Print a float number in normal form, not exponential form / scientific notation [duplicate]
(2 answers)
How to suppress scientific notation when printing float values?
(16 answers)
Closed 2 years ago.
I want to print the whole number instead of 1e-06
number = 1
result = number/1000000
print(result)
Please help whats the best way to do it?
Try out the following by using format:
number = 1
result = number/1000000
print('{0:.6f}'.format(result))
Output:
0.000001
output = f"{num:.9f}"
you can replace 9 with the amount of numbers you have after the decimal point in your number.
and also you will need to define your variable to float to order it will work.
This question already has answers here:
Is floating point math broken?
(31 answers)
Closed 6 years ago.
Am working on a small math-checker using Python and everything works well except of divisions.
Problem: The quotient with two decimals (2/3 = 0.67), float, is equal to an input (0.67). But the if-statement I use to compare a user's input with the result says it isn't equal.
Assumption: the problem is related to float.
My code:
result = float(value0 / value1)
result = round(result,2)
value3 = input("Number 1")
value3 = float(value3)
if result != value3:
print "Wrong!"
print result
elif result == value:
print "Right!"
Of course I could create a function with a different approach, but I am curious to understand why it doesn't work.
If there's a similar thread, please post the link and close this one. Thanks for any help.
when checking floats for equality always use
equal_threshold = 1e-5
if abs(result-value)<equal_threshold:
This question already has answers here:
How can I read inputs as numbers?
(10 answers)
Closed 7 years ago.
I wish to accept a number in float from the user and give back an answer multiplying that number with pi value = 3.14
heres my code:
print "Enter a no."
n = raw_input('enter here: ')
result = 1
def gen_pi(x):
result = x*3.14
return float(result)
gen_pi(n)
its giving me an error of not being able to multiply non int sequence with float. what does it mean? anything wrong with the code?
The result of raw_input is a str which means that n is a str and then in your function, x is a str which can't multiply a float.
you'll want to convert x into a float before multiplying by pi.
There are lots of places to do this, but I'd recommend doing it before passing to gen_pi:
gen_pi(float(n))
This way, gen_pi can always assume it's working with a float and that code can be simplified:
def gen_pi(x):
return x * 3.14
Also note that if you want more precision (and possibly a bit more code clarity), you can use math.pi:
import math
def gen_pi(x):
return x * math.pi
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])