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
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
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)
You can do this with a string with the following VBA in the field calculator:
Left([FIELD_NAME], InStr(1,[FIELD_NAME], ".") - 1)
That will remove the decimal and everything after. If you want some number of digits after the decimal, replace the -1 with + like so:
Left([FIELD_NAME], InStr(1,[FIELD_NAME], ".") + 2)
will give you 2 decimal places. However, this is going to throw errors if some rows have fewer decimal places than you're shortening to. Goldring's answer may be more stable and universal, but if you know your data, you can do it directly in a string field.
If you're trying to get rid of the decimal and everything to the right of it, you should be able to do something like this:
Left([field],InStr([field],".")-1)
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).