How to trim value to two places after dot without rounding, python

后端 未结 3 838
后悔当初
后悔当初 2021-01-29 02:46

I am searching for fastest method to get each value in my column with only two digits after dot without using round()

pd.Series:

input:

3条回答
  •  无人共我
    2021-01-29 03:19

    There are new format specifications, String Format Specification Mini-Language:

    You can do the same as:

    "{0:.2f}".format(1.42345) // output 1.42
    

    Note that the above returns a string. In order to get as float, simply wrap with float(...):

    float("{0:.2f}".format(1.42345)) // output 1.42
    

提交回复
热议问题