Taking the floor of a float

前端 未结 6 538
礼貌的吻别
礼貌的吻别 2021-02-01 13:21

I have found two ways of taking floors in Python:

3.1415 // 1

and

import math
math.floor(3.1415)

The problem

6条回答
  •  天命终不由人
    2021-02-01 14:03

    from math import floor
    
    
    def ff(num, step=0):
        if not step:
            return floor(num)
        if step < 0:
            mplr = 10 ** (step * -1)
            return floor(num / mplr) * mplr
        ncnt = step
        if 1 > step > 0:
            ndec, ncnt = .0101, 1
            while ndec > step:
                ndec *= .1
                ncnt += 1
        mplr = 10 ** ncnt
        return round(floor(num * mplr) / mplr, ncnt)
    

    You can use positive/negative numbers and float points .1, .01, .001...

提交回复
热议问题