以工程格式打印编号 [英] Print number in engineering format

查看:69
本文介绍了以工程格式打印编号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用python将数字打印成工程格式,但似乎无法正常工作。 SEEMS语法很简单,但无法使用。

I am trying to print a number into engineering format with python, but I cannot seem to get it to work. The syntax SEEMS simple enough, but it just doesn't work.

>>> import decimal 
>>> x = decimal.Decimal(1000000)
>>> print x
1000000
>>>> print x.to_eng_string() 
1000000

我不知道为什么会这样。这两个值不相等(一个是字符串,另一个是int)。用十进制设置各种上下文似乎也无济于事。有任何线索或想法吗?

I cannot figure out why this is. The two values are not equal (one is a string, the other is an int). Setting various contexts in decimal doesn't seem to help either. Any clues or ideas?

推荐答案

要使其正常工作,必须先对小数点进行归一化:

To get this to work, you have to normalize the decimal first:

>>> x = decimal.Decimal ('10000000')

>>> x.normalize()
Decimal('1E+7')

>>> x.normalize().to_eng_string()
'10E+6'




原因可以通过深入研究源代码来找到。


The reason for this can be discovered by delving in to the source code.

如果您在Python 2.7中检查了 to_eng_string() .3源代码树( Lib / decimal.py 来自压缩后的源tar球这里),它只需调用 __ str __ 并将 eng 设置为

If you examine to_eng_string() in the Python 2.7.3 source tree (Lib/decimal.py from the gzipped source tar ball here), it simply calls __str__ with eng set to true.

然后您会看到,它决定了最初使用十进制小数点左边的位数:

You can then see that it decides on how many digits go to the left of the decimal initially with:

leftdigits = self._exp + len(self._int)

下表显示了这些值的含义那两件事:

The following table shows what the values are for those two things:

                         ._exp       ._int         len   leftdigits
                         -----       ---------     ---   ----------
Decimal (1000000)            0       '1000000'       7            7
Decimal ('1E+6')             6       '1'             1            7

此后继续执行的代码是:

The code that continues after that is:

if self._exp <= 0 and leftdigits > -6:
    # no exponent required
    dotplace = leftdigits
elif not eng:
    # usual scientific notation: 1 digit on left of the point
    dotplace = 1
elif self._int == '0':
    # engineering notation, zero
    dotplace = (leftdigits + 1) % 3 - 1
else:
    # engineering notation, nonzero
    dotplace = (leftdigits - 1) % 3 + 1

,您可以看到,除非它已经具有在一定范围内的指数( self._exp> 0或左位数<< = -6 ),则不会给出任何指数

and you can see that, unless it already has an exponent in a certain range (self._exp > 0 or leftdigits <= -6), none will be given to it in the string representation.

进一步的调查显示了这种行为的原因。查看代码本身,您将看到它基于 常规十进制算术规范 (PDF 此处)。

Further investigation shows the reason for this behaviour. Looking at the code itself, you'll see it's based on the General Decimal Arithmetic Specification (PDF here).

如果您在该文档中搜索到科学字符串(在该文档上是到工程字符串很大程度上是基于),它在某种程度上(用措辞和粗体表示)表示:

If you search that document for to-scientific-string (on which to-engineering-string is heavily based), it states in part (paraphrased, and with my bold bits):


to-scientific-string如果需要使用指数,操作会使用科学记数法将数字转换为字符串。该操作不受上下文的影响。

The "to-scientific-string" operation converts a number to a string, using scientific notation if an exponent is needed. The operation is not affected by the context.

如果该数字是有限的然后是数字:

If the number is a finite number then:

首先使用从0到9的字符将系数转换为以10为底的字符串,且不带前导零(除非其值为零,在这种情况下为0)

The coefficient is first converted to a string in base ten using the characters 0 through 9 with no leading zeros (except if its value is zero, in which case a single 0 character is used).

接下来,将计算调整后的指数;这是指数,加上转换系数中的字符数,再减去一个。也就是说,指数+(clength-1),其中clength是系数的长度(以十进制数字表示)。

Next, the adjusted exponent is calculated; this is the exponent, plus the number of characters in the converted coefficient, less one. That is, exponent+(clength-1), where clength is the length of the coefficient in decimal digits.

如果指数小于或等于零且调整后的指数大于或等于-6,则数字将转换为字符形式而无需使用指数表示法。在这种情况下,如果指数为零,则不添加小数点。否则(指数为负),将插入一个小数点,该指数的绝对值指定小数点右边的字符数。必要时,将 0字符添加到转换系数的左侧。如果在插入后小数点之前没有字符,那么将使用常规的 0字符作为前缀。

If the exponent is less than or equal to zero and the adjusted exponent is greater than or equal to -6, the number will be converted to a character form without using exponential notation. In this case, if the exponent is zero then no decimal point is added. Otherwise (the exponent will be negative), a decimal point will be inserted with the absolute value of the exponent specifying the number of characters to the right of the decimal point. "0" characters are added to the left of the converted coefficient as necessary. If no character precedes the decimal point after this insertion then a conventional "0" character is prefixed.

换句话说,它正在执行其操作,因为标准告诉它怎么做。

In other words, it's doing what it's doing because that's what the standard tells it to do.

这篇关于以工程格式打印编号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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