将变量的输出显示为2个以上的小数位 [英] Displaying the output of a variable to more than 2 decimal places

查看:319
本文介绍了将变量的输出显示为2个以上的小数位的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 进口时间$ b $我的加法的输出显示在两位小数以下b进口随机

MAX_NUMBER = 1000000.0
random_time = random.randrange(1,MAX_NUMBER-1)/ MAX_NUMBER
range_key = INT(了time.time())+ random_time

range_key
>>> 1347053222.790799

print range_key
>>> 1347053222.79

如何打印完整号码?
如果这是一个函数,我怎么能返回完整的数字?

解决方案

(打印自动执行此操作),python限制它只有12个最重要的数字,再加上小数点。

从函数返回数字时,总是得到完全精确。



要打印更多数字(如果可用),请使用字符串格式化

  print'%f'%range_key#prints 1347053958.526874 

默认为6位数,但您可以指定更高的精度:

  print'%.10f'%range_key#prints 1347053958.5268740654 

或者,python也提供一个更新的字符串格式化方法

  print'{0:.10f}'。format(range_key)#prints 1347053958.5268740654 


I'm trying to display the output of my addition to more than 2 decimal places.

import time
import random

max_number = 1000000.0
random_time = random.randrange(1, max_number-1) / max_number
range_key = int(time.time()) + random_time

range_key
>>> 1347053222.790799

print range_key
>>> 1347053222.79

How can I print the full number? If this were a function, how could I return the full number?

解决方案

When turning a float into a string (printing does this automatically), python limits it to only the 12 most significant digits, plus the decimal point.

When returning the number from a function, you always get the full precision.

To print more digits (if available), use string formatting:

print '%f' % range_key  # prints 1347053958.526874

That defaults to 6 digits, but you can specify more precision:

print '%.10f' % range_key  # prints 1347053958.5268740654

Alternatively, python offers a newer string formatting method too:

print '{0:.10f}'.format(range_key)  # prints 1347053958.5268740654

这篇关于将变量的输出显示为2个以上的小数位的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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