<tfoot id='ffzwH'></tfoot>
<i id='ffzwH'><tr id='ffzwH'><dt id='ffzwH'><q id='ffzwH'><span id='ffzwH'><b id='ffzwH'><form id='ffzwH'><ins id='ffzwH'></ins><ul id='ffzwH'></ul><sub id='ffzwH'></sub></form><legend id='ffzwH'></legend><bdo id='ffzwH'><pre id='ffzwH'><center id='ffzwH'></center></pre></bdo></b><th id='ffzwH'></th></span></q></dt></tr></i><div id='ffzwH'><tfoot id='ffzwH'></tfoot><dl id='ffzwH'><fieldset id='ffzwH'></fieldset></dl></div>
      <bdo id='ffzwH'></bdo><ul id='ffzwH'></ul>

        <small id='ffzwH'></small><noframes id='ffzwH'>

      1. <legend id='ffzwH'><style id='ffzwH'><dir id='ffzwH'><q id='ffzwH'></q></dir></style></legend>

        在 Python 中使用工程符号(带 SI 前缀)将浮点数转换为字符串

        时间:2023-07-02
              <legend id='ddpbQ'><style id='ddpbQ'><dir id='ddpbQ'><q id='ddpbQ'></q></dir></style></legend>
                <bdo id='ddpbQ'></bdo><ul id='ddpbQ'></ul>

                <tfoot id='ddpbQ'></tfoot>
                <i id='ddpbQ'><tr id='ddpbQ'><dt id='ddpbQ'><q id='ddpbQ'><span id='ddpbQ'><b id='ddpbQ'><form id='ddpbQ'><ins id='ddpbQ'></ins><ul id='ddpbQ'></ul><sub id='ddpbQ'></sub></form><legend id='ddpbQ'></legend><bdo id='ddpbQ'><pre id='ddpbQ'><center id='ddpbQ'></center></pre></bdo></b><th id='ddpbQ'></th></span></q></dt></tr></i><div id='ddpbQ'><tfoot id='ddpbQ'></tfoot><dl id='ddpbQ'><fieldset id='ddpbQ'></fieldset></dl></div>

                  <tbody id='ddpbQ'></tbody>

                <small id='ddpbQ'></small><noframes id='ddpbQ'>

                1. 本文介绍了在 Python 中使用工程符号(带 SI 前缀)将浮点数转换为字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我有一个浮点数,例如 x=23392342.1

                  I have a float number such as x=23392342.1

                  我想将其转换为带有工程符号的字符串(带公制前缀)

                  I would like to convert it to a string with engineering notation (with metric prefix)

                  http://en.wikipedia.org/wiki/Engineering_notationhttp://en.wikipedia.org/wiki/Metric_prefix

                  所以在我的例子中 23392342.1 = 23.3923421E6 = 23.3923421 M (mega)

                  So in my example 23392342.1 = 23.3923421E6 = 23.3923421 M (mega)

                  我想显示 23.3923421 M

                  推荐答案

                  这是一个灵感来自 用公制前缀格式化数字?

                  metric.py

                  #!/usr/bin/env python
                  # -*- coding: utf-8 -*-
                  
                  import math
                  
                  
                  def to_si(d, sep=' '):
                      """
                      Convert number to string with SI prefix
                  
                      :Example:
                  
                      >>> to_si(2500.0)
                      '2.5 k'
                  
                      >>> to_si(2.3E6)
                      '2.3 M'
                  
                      >>> to_si(2.3E-6)
                      '2.3 '
                  
                      >>> to_si(-2500.0)
                      '-2.5 k'
                  
                      >>> to_si(0)
                      '0'
                  
                      """
                  
                      inc_prefixes = ['k', 'M', 'G', 'T', 'P', 'E', 'Z', 'Y']
                      dec_prefixes = ['m', '', 'n', 'p', 'f', 'a', 'z', 'y']
                  
                      if d == 0:
                          return str(0)
                  
                      degree = int(math.floor(math.log10(math.fabs(d)) / 3))
                  
                      prefix = ''
                  
                      if degree != 0:
                          ds = degree / math.fabs(degree)
                          if ds == 1:
                              if degree - 1 < len(inc_prefixes):
                                  prefix = inc_prefixes[degree - 1]
                              else:
                                  prefix = inc_prefixes[-1]
                                  degree = len(inc_prefixes)
                  
                          elif ds == -1:
                              if -degree - 1 < len(dec_prefixes):
                                  prefix = dec_prefixes[-degree - 1]
                              else:
                                  prefix = dec_prefixes[-1]
                                  degree = -len(dec_prefixes)
                  
                          scaled = float(d * math.pow(1000, -degree))
                  
                          s = "{scaled}{sep}{prefix}".format(scaled=scaled,
                                                             sep=sep,
                                                             prefix=prefix)
                  
                      else:
                          s = "{d}".format(d=d)
                  
                      return s
                  
                  
                  if __name__ == "__main__":
                      import doctest
                      doctest.testmod()
                  

                  及其用法:

                  from metric import to_si
                  d = 23392342.1
                  print(to_si(d))
                  

                  会显示

                  23.3923421 M
                  

                  这篇关于在 Python 中使用工程符号(带 SI 前缀)将浮点数转换为字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:比较两个包含数字的python字符串 下一篇:Python - 检查字符串中的最后一个字符是否是数字

                  相关文章

                  <tfoot id='P6Fez'></tfoot>
                  1. <small id='P6Fez'></small><noframes id='P6Fez'>

                  2. <legend id='P6Fez'><style id='P6Fez'><dir id='P6Fez'><q id='P6Fez'></q></dir></style></legend>

                      <i id='P6Fez'><tr id='P6Fez'><dt id='P6Fez'><q id='P6Fez'><span id='P6Fez'><b id='P6Fez'><form id='P6Fez'><ins id='P6Fez'></ins><ul id='P6Fez'></ul><sub id='P6Fez'></sub></form><legend id='P6Fez'></legend><bdo id='P6Fez'><pre id='P6Fez'><center id='P6Fez'></center></pre></bdo></b><th id='P6Fez'></th></span></q></dt></tr></i><div id='P6Fez'><tfoot id='P6Fez'></tfoot><dl id='P6Fez'><fieldset id='P6Fez'></fieldset></dl></div>
                      • <bdo id='P6Fez'></bdo><ul id='P6Fez'></ul>