This question already has answers here:
How to display a float with two decimal places?
(13 answers)
Closed 7 years ago.
I have to convert this value to 23.69 or 23.70 not like that talll number 23.69098712446352
a=552.0
b=23.3
c=a/b
print(c)
str.format the output:
print("{:.2f}".format(c))
Or round:
round(c, 2)
Output:
In [9]: print(c)
23.6909871245
In [10]: print("{:.2f}".format(c))
23.69
In [11]: print(round(c, 2))
23.69
Use round function -
c = round(a/b , 2)
Two ways:
round(a.b, 2)
Or just for display use this:
>>> "%.2f" % 3.14159
'3.14'
>>> print("%.2f" % a)
Related
This question already has answers here:
Rounding a number in Python but keeping ending zeros
(6 answers)
Closed 3 months ago.
i want to round off number 7.00087 and output should be 7.000
I tried round() function but it eliminates zero.
This function gives the right output:
def my_round(x, decimals):
str_decimals = str(x % int(x))
return str(int(x)) + str_decimals[1 : 2 + decimals]
What does my_round() do:
1- It retrieves decimals in a string format as a variable named str_decimals
2- Concatenates rounded int(x) with desired decimals and return as output.
Let's apply it:
Input
x = 7.00087
y = my_round(x, 3)
print(y)
Ouput
>>> 7.000
This question already has answers here:
What does & mean in python [duplicate]
(5 answers)
Closed 11 months ago.
print(2 & 3)
I came across a problem statement in one of my technical assessments, I can't understand the usecase of this '&' operator. Can anyone help me, with how this & operator work in python3
& is a bitwise operator, so this is simply where the binary bits line up between 2 and 3
https://wiki.python.org/moin/BitwiseOperators
>>> bin(2)
'0b10'
>>> bin(3)
'0b11'
>>> int("0b10", base=2) # binary string -> int (base10)
2
Here's an example with some bigger numbers
>>> bin(12)
'0b1100'
>>> bin(10)
'0b1010'
>>> 12&10
8
>>> bin(8)
'0b1000'
>>> bin(~8) # NOTE 8 is signed
'-0b1001'
>>> 8&-8
8
This question already has answers here:
How to truncate float values?
(31 answers)
Closed 4 years ago.
I want convert a number to a float number with 3 decimal point but I want it without rounding.
For example:
a = 12.341661
print("%.3f" % a)
This code return this number:
12.342
but I need to original number,I need this:
12.341
I write a code that receive a number form user and convert it to a float number.
I have no idea that what is the number entered with user.
My first thought was to change print("%.3f" % a) to print("%.3f" % (a-0.0005)) but it does not quite work: while it outputs what you want for a=12.341661, if a=12.341 it outputs 12.340, which is obviously not right.
Instead, I suggest doing the flooring explicitly using int():
a = 12.341661
b = int(a*1000)/1000.
print(b)
This outputs what you want:
12.341
To get 3 decimals out even if the input has fewer, you can format the output:
a = 3.1
b = int(a*1000)/1000.
print("%.3f" % b)
Outputs:
3.100
try str and slicing
a = 12.341661
b = str(int(a))
c = str(a - int(a))[1:5]
d = b + c
print(d)
This question already has answers here:
Limiting floats to two decimal points
(35 answers)
Closed 8 years ago.
I am working on a python project and get a problem.
I have a for loop that makes a list with numbers. The problem is when I print out the list to check if the numbers are correct.
Some of the more simpler calculations are:
2.6 * 3 and 1.3 * 9
If you check with a calculator you should get 7.8 and 11.7, but I get 7.800000000000001 and 11.700000000000001
Which is not a big problem, but I don't like how it looks. (First world problem, I know)
Is there any way to fix it?
Use string format?
print "%.2f" % (2.6 * 3)
The %.2f means print a float to 2dp
You can use format to display the output you want:
>>> print(format(2.6 * 3, ".1f"))
7.8
Here ".1f" means "floating point number to one decimal place".
You can use format print.
res = 2.6*3
"%0.2f" % res
Here's a complete example with a list.
>>> n = [1.9, 7.8 , 9,3.4]
>>> print n
[1.8999999999999999, 7.7999999999999998, 9, 3.3999999999999999]
>>> twodecimals = ["%.2f" % v for v in n]
>>> print twodecimals
['1.90', '7.80', '9.00', '3.40']
>>> print twodecimals[0]
1.90
%.2f means prints only 2 decimal points. Similarly, .1f means 1 decimal point and so on.
This question already has answers here:
What is the result of % in Python?
(20 answers)
The % function in Python [duplicate]
(2 answers)
Closed 9 years ago.
What does the percentage
%
function do in python in a line such as
x % 2
In this context it is the modulus, or remainder in the math sense.
So 7 % 2 == 1 because when you divide 7 by 2, you get a remainder of 1.
Similarly, if you wanted the fact that 2 goes into 7 three times, 7 // 2 == 3
The context is important, because % can also be used for old-style string formatting.
In that context, '%s to %s' % ('A', 'Z') would return a string 'A to Z'
However, don't use % for string formatting in python today. Use str.format.
This is called the modulus operator, it gives you the remainder after division:
>>> 5 % 2
1
>>> 44 % 3
2
>>>