Count decimal places in a float [duplicate] - python

This question already has answers here:
Easy way of finding decimal places
(16 answers)
Closed 8 years ago.
In python, I import a file that has different length decimal places in the numbers, such as 7.2 or 7.2332 Is it possible to find out the number of decimal places in the number, if so, how? All the question I can find on SO are about formatting to a specific decimal place, but thats not what I am doing.
The application is to use qdoublespinbox and set the decimal places to only the required amount.
Edit:
In context, I am able to print the number, which will print 7.2, or 5.42422 etc. If the command prompt prints the correct digits (ie. doesnt print everything in the memory allocation of a float) is there any way to get that information.

Assuming that 'import a file' means your decimals are string in a file, you can use reverse and find:
>>> f = "7.2332"
>>> f[::-1].find('.')
4
>>> f = "7.20"
>>> f[::-1].find('.')
2

Convert to a string and find the position of the decimal point, relative to the length of the string.

Related

How to print large numbers in Python? [duplicate]

This question already has answers here:
How to suppress scientific notation when printing float values?
(16 answers)
Closed 3 years ago.
When dividing 2 large numbers in Python the output was 1.5640891676957637e+308. How do I print the entire number?
This can't be done because that's all the precision the float type has. It tells you the first 16 or something digits of your number and the exponent, but the digits after the first 16 have never been calculated because there's no space for them.
If you want to work with huge numbers and have basically infinite precision, almost like with Python's integers, try the SymPy library.
number_str = str(int(1.5640891676957637e+308))
print(number_str)
Prints:
156408916769576373071379516999674270294758197183972476505692635672561429946607721482839700799900977784426920800145985096418278978450607600874550703086464871105809270941181641564392002031609107640705147719606017681794554578537463358952125037388161745430586972528713238507284919924435316681000630776819257180160
You can consider the decimal data type in the standard library https://docs.python.org/3/library/decimal.html "The decimal module provides support for fast correctly-rounded decimal floating point arithmetic. It offers several advantages over the float datatype...Decimal numbers can be represented exactly"

Ways to display money in python without using the decimal package [duplicate]

This question already has answers here:
How can I format a decimal to always show 2 decimal places?
(13 answers)
Closed 5 years ago.
I want to have Python declare a float variable and then display that variable to two decimal points, even if the number is an integer. For example:
def floatToMoney:
# Some kind of function that outputs two decimal places
foo = float(6)
print(floatToMoney(foo))
boo = float(3.14159)
print(floatToMoney(boo))
and have an output of:
6.00
3.14
I know that round() can do this, but again if the number is an integer it doesn't display the decimal places. I found the decimal package, but is there any way to do this without having to import any packages?
I'd just use a format string:
def floatToMoney(f):
return "%.2f" % f
In Python 3.x and 2.7, you can simply do this:
>>> '${:,.2f}'.format(1234.5)
'$1,234.50'
The :, adds a comma as a thousands separator, and the .2f limits the string to two decimal places (or adds enough zeroes to get to 2 decimal places, as the case may be) at the end.

How to perform rounding up or rounding down of a floating point number in python [duplicate]

This question already has answers here:
Round number to nearest integer
(16 answers)
How to round to 2 decimals with Python? [duplicate]
(21 answers)
Closed 6 years ago.
I need to round floating number in such a way that,
If 7.4 comes, it should round to next lower number, that is 7.
If 7.5 or 7.6 comes it should round to next higher number, that is 8
How can I do that? I am using python 2.7
You can use the round() function which comes builtin in python (https://docs.python.org/2/library/functions.html#round)
>>> round(7.4)
7
>>> round(7.5)
8
From the documentation:
Note the behaviour of round() for floats can be surprising: for
example, round(2.675, 2) gives 2.67 instead of the expected
2.68. This is not a bug: it’s a result of the fact that most decimal
fractions can’t be represented exactly as a float. See Floating Point
Arithmetic: Issues and Limitations for more information.
You want to use the round() builtin.
You haven't specified whether or not you are using Python 2 or 3, but note that in Python3, round() does bankers rounding: https://docs.python.org/3/library/functions.html#round.
You can use round:
>>> round(7.4)
7
>>> round(7.5)
8
You can use the round() method for this. The round() method takes two parameters. round(a, b). a is the the floating number whereas b is the number of decimal places up to which you want.
print round(60.23456, 2)
will give you an answer of 60.23
P.S This is python 2.7
In python 3 you can use
math.ceil(x) or math.floor(x)
for more information go to https://docs.python.org/3/library/math.html
Hope this helps :)

Sum of floats: unexpected result [duplicate]

This question already has answers here:
Why does floating-point arithmetic not give exact results when adding decimal fractions?
(31 answers)
Closed 7 years ago.
The following output surprised me:
1.1 + 2.2
=> 3.3000000000000003
An unexpected small digit came up from the sum. The same does not happen for other addends, e.g.:
3.0 + 0.3
=> 3.3
I tried both in Python 2.7 and in 3.4, but the result is the same. What is the reason for this unexpected result of the sum?
Mainly because binary doesn't play well with decimal (2 and 10 are coprime) and floats have limited precision.
Ultimately, when it comes down to it, computers are working with binary numbers. Some fractional numbers do not translate as neat as we would like to binary numbers. The resulting value includes some left-over digital garbage.
For a more complete discussion, see: python floating number and Limiting floats to two decimal points but a reasonable solution might be to specify the desired precision like:
>>> a = 1.1 + 2.2
>>> a = round(a,1)
>>> a
3.3

cout.precision() equivalent for python [duplicate]

This question already has answers here:
Change default float print format
(8 answers)
Closed 8 years ago.
In c++, there exists the cout.precision() to set the number of decimal digits I want to see when I print out a floating point number. Is there any equivalent solution in Python so that every time I want to print out a float I don't get those 11 extra digits that I don't actually need?
In Python you can use the format() function your floats explicitly:
print format(floatvalue, '.5f')
to output a float with 5 digits after the decimal. See the Format Specification Mini-Language for details on the formatting options.
You can also use the str.format() method to control how a float value is interpolated into a larger string, using the same formatting parameters.

Categories