print float to n decimal places including trailing 0's

python float decimal places
python decimal to int
python 3 print float decimal places
python format float keep trailing zeros
how to round float to 2 decimal places
python print float list with 2 decimals
python decimal precision
python float precision

I need to print or convert a float number to 15 decimal place string even if the result has many trailing 0s eg:

1.6 becomes 1.6000000000000000

I tried round(6.2,15) but it returns 6.2000000000000002 adding a rounding error

I also saw various people online who put the float into a string and then added trailing 0's manually but that seems bad...

What is the best way to do this?

For Python versions in 2.6+ and 3.x

You can use the str.format method. Examples:

>>> print('{0:.16f}'.format(1.6))
1.6000000000000001

>>> print('{0:.15f}'.format(1.6))
1.600000000000000

Note the 1 at the end of the first example is rounding error; it happens because exact representation of the decimal number 1.6 requires an infinite number binary digits. Since floating-point numbers have a finite number of bits, the number is rounded to a nearby, but not equal, value.

For Python versions prior to 2.6 (at least back to 2.0)

You can use the "modulo-formatting" syntax (this works for Python 2.6 and 2.7 too):

>>> print '%.16f' % 1.6
1.6000000000000001

>>> print '%.15f' % 1.6
1.600000000000000

python - print float to n decimal places including trailing 0's, For Python versions in 2.6+ and 3.x. You can use the str.format method. Examples: >>> print('{0:.16f}'.format(1.6)) 1.6000000000000001 >  int main {float x = 10.38989445 f; cout < < x < < endl; return 0;} The output would be 10.3899. How to print float numbers with fixed number of decimal places? We can print float numbers with fixed number of decimal places using std::fixed and std::setprecision, these are the manipulators, which are defined in the iomanip header file. Syntax of setprecision

Floating point numbers lack precision to accurately represent "1.6" out to that many decimal places. The rounding errors are real. Your number is not actually 1.6.

Check out: http://docs.python.org/library/decimal.html

print float to n decimal places including trailing 0's, print float to n decimal places including trailing 0's. I need to print or convert a float number to 15 decimal place string even if the result has many trailing 0s eg:. The float () function allows the user to convert a given value into a floating-point number. In this tutorial, you will learn how to convert a number into a floating-point number having a specific number of decimal points in Python programming language. In the above code, the given the float () is a built-in function having a single parameter

I guess this is essentially putting it in a string, but this avoids the rounding error:

import decimal

def display(x):
    digits = 15
    temp = str(decimal.Decimal(str(x) + '0' * digits))
    return temp[:temp.find('.') + digits + 1]

5 Examples of Formatting Float or Double Numbers to String in Java , This will format the floating point number 1.23456 up-to 2 decimal places, is for floating point number, which includes both double and float data type in Java. DecimalFormat.format(), former will always print trailing zeros even if there is no  If the fractional part of the number is zero, no zeros will be printed after the decimal place. Thus there is a difference between 1. 1.000000 The first number is a true 1.0. The second is not 1.0, but has a fractional part that is too small to show in the given precision.

decimal, In decimal floating point, 0.1 + 0.1 + 0.1 - 0.3 is exactly equal to zero. In binary To preserve significance, the coefficient digits do not truncate trailing zeros. If set to 1 , exponents are printed with a capital E ; otherwise, a lowercase e is used​:  round is doing the rouding mathematically, so if last decimal(s) is zero(es) it will omit them from printing. Try with the format method instead: Try with the format method instead: print('${:.2f}'.format(round(floatsList[1], 2)))

number_format - Manual, number_format — Format a number with grouped thousands number_format ( float $number , int $decimals = 0 , string $dec_point = ". Sets the number of decimal points. Only the first byte of each separator was used in older versions. If its length is smaller than number of decimals, add trailing zeros, otherwise  Below is code how you can display an output of float data with 2 decimal places in Java: float ratingValue = 52.98929821f; DecimalFormat decimalFormat = new DecimalFormat("#.##"); float twoDigitsFR = Float.valueOf(decimalFormat.format(ratingValue)); // output is 52.98

format, This is equivalent to a fill character of '0' with an alignment type of '='. The available presentation types for floating point and decimal values are: 'e' In both cases insignificant trailing zeros are removed from the significand, and the decimal  The only issue is the last one where .0 doesn't get removed. But if you are able to live with that then this works best. %.2f will round it to the last 2 decimal digits. So will DecimalFormat. If you need all the decimal places but not the trailing zeros then this works best.

Comments
  • Side note, but that last 2 is not a rounding error exactly. The number 6.2, like many other real numbers, is not exactly representable with a floating point variable in a computer. See docs.python.org/tutorial/floatingpoint.html and stackoverflow.com/questions/1089018/… for more information.
  • @mtrw, I think you could get away with calling it a rounding error - the input is being rounded to the closest binary number.
  • +1. This answer works in >= Python 2.7. In Python 2.6 it would be '{0:.16f}'.format(x), not sure about older Python.
  • @mtrw Good point. The "modulo-formatting" (e.g., '%.16f' % 1.6) works at least back to Python 2.0 (Python 2.0 String Formatting Operations).
  • i get a weird error when i try the above code: # Error: ValueError: zero length field name in format #
  • @jonathantopf Which version of Python do you have? As @mtrw pointed out in a comment above, if you have Python 2.6, you need the positional argument (the 0 in the code of that comment). This is also true if you have Python 3.0.
  • @mtrw I updated the answer to use the positional argument and include the "modulo-formatting" for pre-2.6 interpreters.