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
Answer from alko on Stack OverflowAs 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
Python force round with trailing zeroes with variable in f-string
python round leaving a trailing 0 - Stack Overflow
How to eliminate trailing zeros?
General way to print floats without the .0 part
Is this possible? I need to display 2 decimal points, even if both numbers are zeros. I've tried the {:0.2f}.format method but get an error that numpy doesn't work with strings.
This is my current code and output.
print("Observed Prices: ",np.round(y_test_1[0:10],2))
print("Estimated Prices:",np.round(test_pred_1[0:10],2))Observed Prices: [33 45 54 38 22 47 38 51 46 47]
Estimated Prices: [19. 20. 24. 21. 21. 21. 18. 22. 23. 20.]
Pass the rounded value to int() to get rid of decimal digits:
>>> value = 10.01
>>> int(round(value))
10
>>> value = 10.55
>>> int(round(value))
11
10.0 and 10 are the same float value. When you print that value, you get the string 10.0, because that's the default string representation of the value. (The same string you get by calling str(10.0).)
If you want a non-default representation, you need to ask for it explicitly. For example, using the format function:
print format(rounded_value, '.0f')
Or, using the other formatting methods:
print '{:.0f}'.format(rounded_value)
print '%.0f' % (rounded_value,)
The full details for why you want '.0f' are described in the Format Specification Mini-Language, but intuitively: the f means you want fixed-point format (like 10.0 instead of, say, 1.0E2), and the .0 means you want no digits after the decimal point (like 10 instead of 10.0).
Meanwhile, if the only reason you rounded the value was for formatting… never do that. Leave the precision on the float, then trim it down in the formatting:
print format(value, '.0f')
I have a float formatted to 2 decimal places. I need to eliminate the 2nd decimal place if it's a "0" but still keep 2 decimal places open for when its 2 whole numbers.
number = float(25.20458)
print(format(number, ".2f"))
#Comes out as 25.20
#Need 25.2Windows 10 and Python 3.7