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!
Answer from Paulo Bu on Stack OverflowWhat 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
Videos
I am looking to manipulate a data frame of floats which all need 6 decimal points after manipulation.
I am looking to add brackets and () around the floats based on conditionals which is why I need to convert to strings. I then can concat the two strings together
However when I convert to str, it reduces the number of decimals to 2.
For example
-35.920000 Original Dataframe
Converted to str
-35.92 After conversion
If I convert the string back to a float, it does not retain the 6 decimals from the original df.
My understanding is both values are stored the same and they both are logically = when checked in the notebook , but for management reasons I am trying to see if there is a way to coerce the string method the take a literal copy of the float, rather than reducing it down.
Sorry for the formatting, I am on mobile .
Thanks
Using %f format specifier:
('%.2f' % (value,)).rstrip('0').rstrip('.')
Using round() function:
str(round(value)).rstrip('0').rstrip('.')
Use round together with %g -- you want at most 2 digits shown, so round to two digits, then use %g to print it as short as possible:
>>> "%g" % round(20.016, 2)
'20.02'
>>> "%g" % round(20, 2)
'20'
See PEP 3101:
'g' - General format. This prints the number as a fixed-point
number, unless the number is too large, in which case
it switches to 'e' exponent notation.
Old style (not preferred):
>>> "%g" % float(10)
'10'
New style:
>>> '{0:g}'.format(float(21))
'21'
New style 3.6+:
>>> f'{float(21):g}'
'21'
rstrip doesn't do what you want it to do, it strips any of the characters you give it and not a suffix:
>>> '30000.0'.rstrip('.0')
'3'
Actually, just '%g' % i will do what you want.
EDIT: as Robert pointed out in his comment this won't work for large numbers since it uses the default precision of %g which is 6 significant digits.
Since str(i) uses 12 significant digits, I think this will work:
>>> numbers = [ 0.0, 1.0, 0.1, 123456.7 ]
>>> ['%.12g' % n for n in numbers]
['1', '0', '0.1', '123456.7']
For pandas >= 1.0:
<NA> type was introduced for 'Int64'. You can now do this:
df['your_column'].astype('Int64').astype('str')
And it will properly convert 1.0 to 1.
Alternative:
If you do not want to change the display options of all pandas, @maxymoo solution does, you can use apply:
df['your_column'].apply(lambda x: f'{x:.0f}')
Converting to int (i.e. with .astype(int).astype(str)) won't work if your column contains nulls; it's often a better idea to use string formatting to explicitly specify the format of your string column; (you can set this in pd.options):
>>> pd.options.display.float_format = '{:,.0f}'.format
>>> df.astype(float).sum()
0 7
1 4
2 11
dtype: float64
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))
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'
I tried
n1=input('First number')
n2=input('Second number')
sum = float(n1) + float(n2)
str(sum)
print('The sum of the values is: ' + sum)My error is:
TypeError: can only concatenate str (not "float") to str
I tried googling this error and got some answers like print(f' which I didn't really understand, and some others that looked a little complicated, I am very new.
I am trying to improve my googling skills.