python函数来设置float的精度 [英] python function to set accuracy of float

查看:1295
本文介绍了python函数来设置float的精度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  def准确性(数字,指数):


$ b

例如

$ ul
$ li $ code>精确度(2.5e-10,-5)将会返回0.

  • 准确度(49,2)
  • 准确性(50,2)会返回100。



    所以基本上它会到指数最接近的10次方 index
    你会怎么做?



  • 解决方案

      def accuracy(n,i): 
    return round(float(n)/ 10 ** i)* 10 ** i


    I would like to make a function:

    def accuracy(number, index):
    

    For example

    • accuracy(2.5e-10, -5) would return 0.
    • accuracy(49, 2) would return 0.
    • accuracy(50, 2) would return 100.

      So basically it would round to the closest 10 power of the index index How would you do that?

    解决方案

    def accuracy(n, i):
        return round(float(n) / 10**i) * 10**i
    

    这篇关于python函数来设置float的精度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆