How to print a given number of decimal places? [duplicate] - python

This question already has answers here:
How to print float to n decimal places including trailing 0s?
(5 answers)
Closed 3 years ago.
I want to print my result with a specific number of decimal places inside of a for loop where the value of it is the number of decimal places to be printed.
Below is a sample of the relevant part of the code:
for i in range (-15, -7):
print ('Valor do erro:' , 10**i, 'Valor da serie:', count, '------->', '%.16f' % adder(count))

Format-specifiers can be nested:
>>> for i in range(1, 5):
... print("{:.{}f}".format(1/i, i))
...
1.0
0.50
0.333
0.2500
Here, 1/i goes to the first (outer) {...} and i to the second (inner) {}.
Note that the number of decimal places can not be negative, though. For this, you might just want to use scientific notation instead.
>>> for i in range(-2, 3):
... print("{:.2e}".format(10**i))
...
1.00e-02
1.00e-01
1.00e+00
1.00e+01
1.00e+02

Related

is there a function in python to round off three digits after decimal but show all three digits even if zero [duplicate]

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

How do I keep terminal zeroes when rounding to a set number of decimal places? [duplicate]

This question already has answers here:
How to format a floating number to fixed width in Python
(10 answers)
Closed 3 years ago.
Problem requires to output decimal proportion of negative integers in an unknown list. The desired answer is 0.500000 (6 decimal places), but my program only outputs 0.5. How do I keep the 5 terminal zeroes?
I've tried using the round(number, ndigits) function and getcontext().prec from the Decimal library. All of these have yielded just 0.5.
def plusMinus(arr):
from decimal import getcontext, Decimal
getcontext().prec = 6
negamt = 0
posamt = 0
zeroamt = 0
for i in arr:
if i < 0:
negamt += 1
elif i > 0:
posamt += 1
else:
zeroamt += 1
print(Decimal(posamt)/Decimal(n), Decimal(negamt)/Decimal(n), Decimal(zeroamt)/Decimal(n))
The expected results for the unknown array are 0.500000, 0.333333, 0.166667; my program returns 0.5, 0.333333, 0.166667.
You want to change the print format. If you want six digits after the decimal point, you can use this format string:
>>> print('{:1.6f}'.format(0.5)})
0.500000

limited float decimal point without rounding the number [duplicate]

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)

How do remove trailing numbers in python [duplicate]

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.

float deviation in python list [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Python float - str - float weirdness
I run the following code in python on codepad.org:
num = 1.6
print num
list = [num]
print list
num2 = list[0]
print num2
And I get the following output:
1.6
[1.6000000000000001]
1.6
Why the tiny deviation in the list?
list.__str__ calls repr on its elements, where as print calls str:
>>> str(1.6)
'1.6'
>>> repr(1.6)
'1.6000000000000001'
Since floating point numbers are not guaranteed to be exact (and cannot be exact for values that can't be expressed as a * 2b for integers a,b), both representations are correct, or, in other words:
>>> 1.6 == 1.6000000000000001
True

Categories