floor and ceil with number of decimals

前端 未结 3 1599
情话喂你
情话喂你 2021-01-05 06:51

I need to floor a float number with an specific number of decimals.

So:

2.1235 with 2 decimals --> 2.12
2.1276 with 2 decimals --> 2.12  (round         


        
3条回答
  •  傲寒
    傲寒 (楼主)
    2021-01-05 07:39

    This seems to work (needs no import and works using the // operator which should be faster than numpy, as it simply returns the floor of the division):

    a = 2.338888
    n_decimals = 2
    a = ((a*10**n_decimals)//1)/(10**n_decimals)
    

提交回复
热议问题