cout.precision() equivalent for python [duplicate] - python

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.

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.

Python - Binary to decimal [duplicate]

This question already has answers here:
Converting integer to binary in python
(17 answers)
Convert base-2 binary number string to int
(10 answers)
Closed 6 years ago.
I don't know much about Python, so sorry if this sounds silly. I am getting a string of 0s and 1s from an Arduino, and I need to convert it to a 'regular' number. I've found out how to convert a binary string to a decimal integer pretty easily, but how would I convert it if my binary string is stored in a variable ?
Let's say I've got a=00000110
int(a, 2) doesn't work (tells me "int() can't convert non-string with explicit base")
Is there a specific syntax that I should be aware of ? Can't I put a variable in here ?
Thanks in advance !

Python String Parsing Floats [duplicate]

This question already has answers here:
Convert fraction to float?
(9 answers)
Closed 8 years ago.
How to change a '14/15' string to a float?
I am trying to extract data from a text file would like to convert '1/3' to a float. float('1/3') doesn't work. I was thinking about splitting into two parts at '/' by 1 and 3 then dividing, but it seems cludgy. Is there a more pythonic way to do this? I'm using Python 2.7
If you only ever need to evaluate simple X/Y fractions:
s = "14/15"
num, denom = map(float, s.split("/", 1))
print(num / denom)
If you need a more complete expression evaluator, take a look at the asteval module.
Using eval() might also see like a nice easy way to do it, but I'd advise against it for security reasons.
If you trust your input:
from __future__ import division
eval('14/15')

Count decimal places in a float [duplicate]

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.

Categories