Remove Last Number of a Integer
Question:
I have searched for a few answers and none of them seems to work.
Below is an example but the code does not work and gives errors. I am using Python 2.7.
operationTwo = 91239
operationTwo = operationTwo[:-1]
print(operationTwo)
Answers:
That is very simple:
operationTwo = int(operationTwo / 10)
The code you found which is slicing
, works but not on integers
. If you want to use it you can convert the number to str
for slicing then convert it back to int
. It is not the best practice but it can be done as the following:
operationTwo = 91239
operationTwo = int(str(operationTwo)[:-1])
print(operationTwo)
I would however go with integer
division, like:
operationTwo = 91239
operationTwo = operationTwo // 10
print(operationTwo)
I want to remove last three digits from a timestamp and Mohd’s answer work for me.
Before using this I got this error OSError: [Errno 22] Invalid argument
data = 1659347766123
data = int(str(data)[:-3])
print(data)
timeStamp = datetime.fromtimestamp(data)
print(timeStamp)
// 1659347766
// 2022-08-01 15:26:06
I have searched for a few answers and none of them seems to work.
Below is an example but the code does not work and gives errors. I am using Python 2.7.
operationTwo = 91239
operationTwo = operationTwo[:-1]
print(operationTwo)
That is very simple:
operationTwo = int(operationTwo / 10)
The code you found which is slicing
, works but not on integers
. If you want to use it you can convert the number to str
for slicing then convert it back to int
. It is not the best practice but it can be done as the following:
operationTwo = 91239
operationTwo = int(str(operationTwo)[:-1])
print(operationTwo)
I would however go with integer
division, like:
operationTwo = 91239
operationTwo = operationTwo // 10
print(operationTwo)
I want to remove last three digits from a timestamp and Mohd’s answer work for me.
Before using this I got this error OSError: [Errno 22] Invalid argument
data = 1659347766123
data = int(str(data)[:-3])
print(data)
timeStamp = datetime.fromtimestamp(data)
print(timeStamp)
// 1659347766
// 2022-08-01 15:26:06