用Python中的工程单位打印数字

编程入门 行业动态 更新时间:2024-10-28 19:35:48
本文介绍了用Python中的工程单位打印数字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

可能重复: 以工程格式打印编号

如何以科学计数法打印数字的幂是3的倍数?例如:

How do I print numbers in scientific notation with powers that are multiples of 3? For example:

1.5e4 --> 15e3 1.27e-8 --> 12.7e-9 2.9855e-11 --> 29.855e-9

我正在寻找类似于计算器上 ENG 按钮的功能.某个地方有Python软件包可以做到这一点吗?

I'm looking for a function similar to the ENG button on a calculator. Is there a Python package somewhere that does this?

推荐答案

似乎还没有这样的功能(至少在Python 2.7中如此),请参见: bugs.python/issue8060 在页面上 bytes/topic/python/answers/616948-string-formatting-engineering-notation 我找到了以下解决方案(我个人不太喜欢,但似乎可行):

It appears there isn't such a feature yet (at least in Python 2.7), see: bugs.python/issue8060 On the page bytes/topic/python/answers/616948-string-formatting-engineering-notation I found the following solution (which I personally don't like that much, but seems to work):

import math for exponent in xrange(-10, 11): flt = 1.23 * math.pow(10, exponent) l = math.log10(flt) if l < 0: l = l - 3 p3 = int(l / 3) * 3 multiplier = flt / pow(10, p3) print '%e =%fe%d' % (flt, multiplier, p3)

只需根据您的需要进行调整即可.

Just adapt it according to your needs.

也请点击此处以工程格式打印编号

更多推荐

用Python中的工程单位打印数字

本文发布于:2023-08-07 07:02:33,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1317640.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:单位   数字   工程   Python

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!