简体   繁体   English

python 中如何隐含地决定科学记数法包含?

[英]How scientific notation inclusion is decided implicitly in python?

What are the criterias in python for scientific notation inclusion in a calculation result? python 在计算结果中包含科学计数法的标准是什么?

d1 = Decimal('500')
d2 = Decimal('0.25')
print d1/d2

This gives the result 2.0E+3这给出了结果2.0E+3

If i include 2 zeroes after decimal point for 500, that is,如果我在 500 的小数点后包含 2 个零,即

d1 = Decimal('500.00')
d2 = Decimal('0.25')
print d1/d2

This gives the result 2000这给出了结果2000

Also how can it be made sure that there are no scientific notation in the result?另外如何确保结果中没有科学记数法?

See FAQ in the official docs:请参阅官方文档中的常见问题解答

Q. Some decimal values always print with exponential notation.问:某些十进制值总是以指数表示法打印。 Is there a way to get a non-exponential representation?有没有办法获得非指数表示?

A. For some values, exponential notation is the only way to express the number of significant places in the coefficient. A. 对于某些值,指数符号是表示系数中有效位数的唯一方法。 For example, expressing 5.0E+3 as 5000 keeps the value constant but cannot show the original's two-place significance.例如,将 5.0E+3 表示为 5000 保持值不变,但不能显示原件的两位显着性。

If an application does not care about tracking significance, it is easy to remove the exponent and trailing zeroes, losing significance, but keeping the value unchanged:如果应用程序不关心跟踪重要性,很容易删除指数和尾随零,失去重要性,但保持值不变:

def remove_exponent(d):
    return d.quantize(Decimal(1)) if d == d.to_integral() else d.normalize()
>>> remove_exponent(Decimal('5E+3'))
Decimal('5000')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM