Add Zeros to a Float After the Decimal Point in Python

Add zeros to a float after the decimal point in Python

Format it to 6 decimal places:

format(value, '.6f')

Demo:

>>> format(2.0, '.6f')
'2.000000'

The format() function turns values to strings following the formatting instructions given.

Rounding a number in Python but keeping ending zeros

As you are talking about trailing zeros, this is a question about representation as string,
you can use

>>> "%.2f" % round(2606.89579999999, 2)
'2606.90'

Or use modern style with format function:

>>> '{:.2f}'.format(round(2606.89579999999, 2))
'2606.90'

and remove point with replace or translate (_ refers to result of previous command in python console):

>>> _.translate(None, '.')
'260690'

Note that rounding is not needed here, as .2f format applyies the same rounding:

>>> "%.2f" % 2606.89579999999
'2606.90'

But as you mentioned excel, you probably would opt to roll your own rounding function, or use decimal, as float.round can lead to strange results due to float representation:

>>> round(2.675, 2)
2.67
>>> round(2606.89579999999, 2)
2606.89

With decimal use quantize:

>>> from decimal import *
>>> x = Decimal('2606.8950000000001')
# Decimal('2606.8950000000001')
>>> '{}'.format(x.quantize(Decimal('.01'), rounding=ROUND_HALF_EVEN))
'2606.90'

That, for your original task, becomes:

>>> x = Decimal('2606.8950000000001')
>>> int((x*100).quantize(1, rounding=ROUND_HALF_EVEN))
260690

And the reason of strange rounding comes to the front with Decimal:

>>> x = Decimal(2606.8950000000001)
# Decimal('2606.89499999999998181010596454143524169921875') # internal float repr

How to format a float in python which has variable number of zeros after decimal point?

There is no built-in support for this. That is, there is no format specifier or conversion function that does this directly. You will need to write your own code to do the formatting.

How to add trailing zeroes to floating point value and export to excel as float(number)?

You could try df['column']=df['column'].astype(float). This would change for a float number.

If you back to string, Excel read as text.

How to print float to n decimal places including trailing 0s?

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

Format a number containing a decimal point with leading zeroes

Starting with a string as your example does, you could write a small function such as this to do what you want:

def zpad(val, n):
bits = val.split('.')
return "%s.%s" % (bits[0].zfill(n), bits[1])

>>> zpad('3.3', 5)
'00003.3'

Fixed digits after decimal with f-strings

Include the type specifier in your format expression:

>>> a = 10.1234
>>> f'{a:.2f}'
'10.12'

How to get numbers after decimal point?

An easy approach for you:

number_dec = str(number-int(number))[1:]


Related Topics



Leave a reply



Submit