python:round / format float / string到特定的小数/分数

时间:2018-06-06 14:25:58

标签: python decimal rounding fractions floor

我发现了很多关于python中舍入的帖子,但是根据我的理解,到目前为止还没有解决我的问题。

我正在寻找两种解决方案:

  • 圆形最近:浮点数/字符串数字需要根据其大小进行不同的舍入。

  • 向下舍入:浮动/字符串数字需要根据其大小不同地舍入 down

  • 向上舍入:浮点数/字符串数字需要根据其大小不同地舍入 up

示例:

  • +500:0小数(整数)(例如,最接近:500.5 - > 501;向下:500.99 - > 500;向上:500.01 - > 501)
  • +100:1小数(分数:0.2)(例如,最接近:100.29 - > 100.2;向下:100.39 - > 100.2;向上:100.21 - > 100.4)。因此,该值必须以.0,.2,.4,.6或.8
  • 结尾
  • +5:2小数(分数:0.01)(例如,最接近:5.019-> 5.02;下:5.019-> 5.01;上:5.011-> 5.02)。因此,该值必须以.x0,.x1,.x2,.x3,.x4,.x5,.x6,.x7,.x8或.x9
  • 结尾

结果变量应该是一个字符串。

我正在寻找的整洁的东西是一个函数,我可以传递小数位数小数

2 个答案:

答案 0 :(得分:1)

您可以使用removed进行高度自定义Decimal module

import decimal 

def rnd(f,n,r=decimal.ROUND_HALF_DOWN):
    pat='1.'+'0'*n
    return str(decimal.Decimal(str(f)).quantize(decimal.Decimal(pat),r))

现在测试:

for n in (500.99,100.39,5.019):
    for i in (0,1,2):
        print('n={:10}, i={:2}, Down: {:>10}, Closest: {:>10}'.format(n,i, rnd(n,i,decimal.ROUND_FLOOR),rnd(n,i)))   

打印:

n=    500.99, i= 0, Down:        500, Closest:        501
n=    500.99, i= 1, Down:      500.9, Closest:      501.0
n=    500.99, i= 2, Down:     500.99, Closest:     500.99
n=    100.39, i= 0, Down:        100, Closest:        100
n=    100.39, i= 1, Down:      100.3, Closest:      100.4
n=    100.39, i= 2, Down:     100.39, Closest:     100.39
n=     5.019, i= 0, Down:          5, Closest:          5
n=     5.019, i= 1, Down:        5.0, Closest:        5.0
n=     5.019, i= 2, Down:       5.01, Closest:       5.02

(注意:对于1个十进制和100.39的输入,这是不同的输出。请说明在这种情况下100.2的所需输出?输出不应该是{{1 }}?)

通过澄清,我认为您只需要使用100.3

fmod

适用于所有给出的例子:

import math 

def rnd(n,f,prec,direction='down'):
    if direction=='down':
        if prec==0:
            return str(int(n))
        return str(round(n-math.fmod(n,f),prec))
    else:
        if prec==0:
            return str(int(round(n)))
        n+=f
        return str(round(n-math.fmod(n,f),prec))

(使用>>> rnd(500.99,0.1,0) '500' >>> rnd(500.99,0.1,0,'') '501' >>> rnd(100.39,0.2,1,'') '100.4' >>> rnd(100.39,0.2,1) '100.2' >>> rnd(100.59,0.2,1,'') '100.6' >>> rnd(100.59,0.2,1) '100.4' >>> rnd(5.019,0.01,2) '5.01' >>> rnd(5.019,0.01,2,'') '5.02' 关键字参数调用带有除direction=以外的任何字符串值的函数,以便起床。根据自己的喜好进行更改...)

编辑2

返回最接近输入数字的方法:

'down'

答案 1 :(得分:0)

这样的事情怎么样?

extension UIApplication {
    var statusBarView: UIView? {
        return value(forKey: "statusBar") as? UIView
    }
}


struct StatusBar {

    static func setStatusBar() {
        let appDelegate = UIApplication.shared.delegate as? AppDelegate
        if let currentVC = appDelegate?.window?.rootViewController?.getCurrentlyDisplayedVC() {
            if currentVC is LoginVC || currentVC is SignUpVC || currentVC is SignUpVC {
                UIApplication.shared.statusBarView?.backgroundColor = .clear
            }
            else {
                UIApplication.shared.statusBarView?.backgroundColor = Colors.mainBlueColor
            }
        }
        UIApplication.shared.statusBarStyle = .lightContent
    }
}



extension UIViewController {

    func getCurrentlyDisplayedVC() -> UIViewController {

        if let presentedVC = presentedViewController {
            return presentedVC.getCurrentlyDisplayedVC()
        }
        else if let split = self as? UISplitViewController, let last = split.viewControllers.last {
            return last.getCurrentlyDisplayedVC()
        }
        else if let nav = self as? UINavigationController, let top = nav.topViewController {
            return top.getCurrentlyDisplayedVC()
        }
        else if let tab = self as? UITabBarController {

            if let selected = tab.selectedViewController {
                return selected.getCurrentlyDisplayedVC()
            }
        }
        return self
    }
}