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.
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.
From Python 3.6 it's also possible to do f-string formatting. This looks like:
f"{value:.6f}"
Example:
> print(f"{2.0:.6f}")
'2.000000'
python - Adding zeros after the decimal point - Stack Overflow
keep trailing 0's in floats?
python - How to add zeros after the decimal to make 2 digits after decimal in ipython and pyspark - Stack Overflow
format - python padding decimals with zeros - Stack Overflow
trying to make a money calculator, so I want to keep 0's
10.10 instead of 10.1
do I have to converting to a string for printing and use {2:g} or can I get floats to show 0's?
EDIT
solved,
format(number, '.2f' )
unlesss thers a better way?
To format a floating point number with a given precision you should use the 'f' specifier. Without it the value is formatted as a string.
>>> '{:.4f}'.format(4.1)
'4.1000
You can also specify the minimum width of the complete output and the fill character:
>>> '{:07.4f}'.format(4.1)
'04.1000'
Here the output is padded on the left with zeros to be at least 7 characters.
'{:5.5f}'.format(4.1)
The f in the format string makes the difference here. You can read more about the formatters in here, important for this task is
'f' Fixed point. Displays the number as a fixed-point number. The default precision is 6.
But you wanted just 4 digits after the comma, so better is
>>> '{:.4f}'.format(4.1)
'4.1000'
Multiply by 10 every time you want to add another 0.
This is the same as saying 10 to the power of how many zeroes you want. In python, that would be number * (10**extraZeroCount)
As I understand the question, the 3 extra zeros are for output purposes only. There is no need to obtain integers back. Strings (or for that mater, any other type) is enough. In this vein, any of
print(a*1000)
print(str(a)+"000")
print(a,"000",sep="")
and perhaps several others, would work.
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 applies 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
As of Python 3.6, you can also use an f-string to inline format the number. In this case, the desired format is floating point with 2 decimal places so you would use .2f as the format specifier:
x = 2606.89579999999
x = round(x, 2) # not strictly necessary as format will round for you
print(f'{x:.2f}')
Output:
2606.90