You could just remove the '.' between the digits:
s = '0.0.1'
s = s.replace('.', '')
after that you can make it an int:
int(s)
By making it an integer, you will also remove any leading zeros. If you need a string afterwards just convert it back to string:
s = str(int(s))
Answer from Juergen on Stack OverflowYou could just remove the '.' between the digits:
s = '0.0.1'
s = s.replace('.', '')
after that you can make it an int:
int(s)
By making it an integer, you will also remove any leading zeros. If you need a string afterwards just convert it back to string:
s = str(int(s))
You could use join and a comprehension:
>>> s = '0.0.1'
>>> ''.join(c for c in s if c != '.')
'001'
If you want to strip the leading 0s:
>>> str(int(''.join(c for c in s if c != '.')))
'1'
What about converting it to int?
>>>int(a)
100
Just for the sake of completeness, there are many many ways to remove the decimal part from a string representation of a decimal number, one that I can come up right now is:
s='100.0'
s=s[:s.index('.')]
s
>>>'100'
Perhaps there's another one more simple.
Hope this helps!
If you do not want to convert it to an int you can also split it.
>>> a = 100.25
>>> str(a).split('.')[0]
>>> '100' # result is now a string
Int to String without decimal points?
Python: Remove division decimal - Stack Overflow
integer - Python how to remove decimal? - Stack Overflow
How to terminate or remove the .0 point from an int.
I need to remove decimals from float to get 6 characters after the dot WITHOUT rounding For example I have 0.00655379 and I need to get 0.006553
You can call int() on the end result:
>>> int(2.0)
2
When a number as a decimal it is usually a float in Python.
If you want to remove the decimal and keep it an integer (int). You can call the int() method on it like so...
>>> int(2.0)
2
However, int rounds down so...
>>> int(2.9)
2
If you want to round to the nearest integer you can use round:
>>> round(2.9)
3.0
>>> round(2.4)
2.0
And then call int() on that:
>>> int(round(2.9))
3
>>> int(round(2.4))
2
You would need to reassign x to the value of x = int(x) or you could also use str.format if you just want the output formatted:
print "Het antwoord van de berekening is: {:.0f}.".format(x)
int and round will exhibit different behaviour, if you have anything >= 5 after the decimal point then int will floor but round will round up, if you want to actually use round you might want to combine the two:
In [7]: x = round(1.5)
In [8]: x
Out[8]: 2.0
In [9]: int(x)
Out[9]: 2
Or again combine with str.format:
In [10]: print "Het antwoord van de berekening is: {:.0f}".format(round(1.5))
Het antwoord van de berekening is: 2
The round() function cannot alter the x variable in place, as numbers are immutable. Instead, the rounded result is returned, which your code ignores.
Store the result back in x:
x = round(x)
This will give you a floating point number rounded to the nearest whole number.
Alternatively, use x = int(x), which gives you an integer number, but floors that number (removes the decimal portion regardless if it is closer to the next whole number or not).
Hello everyone,
I am still new to python and learning.
So I practiced some exercises and made an app that calculates the percentage from the number the user enters.
My question use, how can I terminate the .0 part if the user enters an Int and keep the decimal part if they enter a float?
so for example, 5% of 100 is 5 ( Int)
and 5.1% of 100 is 5.1 (float)