string formatting: engineering notation

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Darren Dale

    string formatting: engineering notation

    Does anyone know if it is possible to represent a number as a string with
    engineering notation (like scientific notation, but with 10 raised to
    multiples of 3: 120e3, 12e-6, etc.). I know this is possible with the
    decimal.Decimal class, but repeatedly instantiating Decimals is inefficient
    for my application (matplotlib plotting library). If it is not currently
    possible, do you think the python devs would be receptive to including
    support for engineering notation in future releases?

    Thanks,
    Darren
  • Steve Holden

    #2
    Re: string formatting: engineering notation

    Darren Dale wrote:
    Does anyone know if it is possible to represent a number as a string with
    engineering notation (like scientific notation, but with 10 raised to
    multiples of 3: 120e3, 12e-6, etc.). I know this is possible with the
    decimal.Decimal class, but repeatedly instantiating Decimals is inefficient
    for my application (matplotlib plotting library). If it is not currently
    possible, do you think the python devs would be receptive to including
    support for engineering notation in future releases?
    >
    How close is this:
    >>"%.3e" % 3.14159
    '3.142e+00'

    regards
    Steve
    --
    Steve Holden +44 150 684 7255 +1 800 494 3119
    Holden Web LLC/Ltd http://www.holdenweb.com
    Skype: holdenweb http://del.icio.us/steve.holden
    Blog of Note: http://holdenweb.blogspot.com
    See you at PyCon? http://us.pycon.org/TX2007

    Comment

    • Darren Dale

      #3
      Re: string formatting: engineering notation

      Steve Holden wrote:
      Darren Dale wrote:
      >Does anyone know if it is possible to represent a number as a string with
      >engineering notation (like scientific notation, but with 10 raised to
      >multiples of 3: 120e3, 12e-6, etc.). I know this is possible with the
      >decimal.Decima l class, but repeatedly instantiating Decimals is
      >inefficient for my application (matplotlib plotting library). If it is
      >not currently possible, do you think the python devs would be receptive
      >to including support for engineering notation in future releases?
      >>
      How close is this:
      >
      >>"%.3e" % 3.14159
      '3.142e+00'
      >>"%.3e" % 31415.9
      '3.142e+04'

      What I am looking for is '31.4159e+03'

      Darren

      Comment

      • Grant Edwards

        #4
        Re: string formatting: engineering notation

        On 2007-03-14, Steve Holden <steve@holdenwe b.comwrote:
        Darren Dale wrote:
        >Does anyone know if it is possible to represent a number as a string with
        >engineering notation (like scientific notation, but with 10 raised to
        >multiples of 3: 120e3, 12e-6, etc.). I know this is possible with the
        >decimal.Decima l class, but repeatedly instantiating Decimals is inefficient
        >for my application (matplotlib plotting library). If it is not currently
        >possible, do you think the python devs would be receptive to including
        >support for engineering notation in future releases?
        >>
        How close is this:
        >
        >>"%.3e" % 3.14159
        '3.142e+00'
        Not close at all. It should be "3.14159"
        >>"%.3e" % 31.4159
        '3.142e+01'

        should be 31.4159
        >>"%.3e" % 314.159
        '3.142e+02'

        should be 314.159
        >>"%.3e" % 31415.9
        '3.142e+04'

        should be 31.4159e3

        --
        Grant Edwards grante Yow! LOU GRANT froze
        at my ASSETS!!
        visi.com

        Comment

        • Jorge Godoy

          #5
          Re: string formatting: engineering notation

          Steve Holden <steve@holdenwe b.comwrites:
          Darren Dale wrote:
          >Does anyone know if it is possible to represent a number as a string with
          >engineering notation (like scientific notation, but with 10 raised to
          >multiples of 3: 120e3, 12e-6, etc.). I know this is possible with the
          >decimal.Decima l class, but repeatedly instantiating Decimals is inefficient
          >for my application (matplotlib plotting library). If it is not currently
          >possible, do you think the python devs would be receptive to including
          >support for engineering notation in future releases?
          >>
          How close is this:
          >
          >>"%.3e" % 3.14159
          '3.142e+00'
          >>"%.3e" % 314159
          '3.142e+05'
          >>>
          Not close when you have the exponent.



          --
          Jorge Godoy <jgodoy@gmail.c om>

          Comment

          • attn.steven.kuo@gmail.com

            #6
            Re: string formatting: engineering notation

            On Mar 14, 1:14 pm, Darren Dale <d...@cornell.e duwrote:
            Does anyone know if it is possible to represent a number as a string with
            engineering notation (like scientific notation, but with 10 raised to
            multiples of 3: 120e3, 12e-6, etc.). I know this is possible with the
            decimal.Decimal class, but repeatedly instantiating Decimals is inefficient
            for my application (matplotlib plotting library). If it is not currently
            possible, do you think the python devs would be receptive to including
            support for engineering notation in future releases?

            Do you also consider this to be too inefficient?


            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)

            --
            Hope this helps,
            Steven

            Comment

            • Darren Dale

              #7
              Re: string formatting: engineering notation

              attn.steven.kuo @gmail.com wrote:
              On Mar 14, 1:14 pm, Darren Dale <d...@cornell.e duwrote:
              >Does anyone know if it is possible to represent a number as a string with
              >engineering notation (like scientific notation, but with 10 raised to
              >multiples of 3: 120e3, 12e-6, etc.). I know this is possible with the
              >decimal.Decima l class, but repeatedly instantiating Decimals is
              >inefficient for my application (matplotlib plotting library). If it is
              >not currently possible, do you think the python devs would be receptive
              >to including support for engineering notation in future releases?
              >
              >
              Do you also consider this to be too inefficient?
              >
              >
              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)
              >
              That's a good suggestion. It's probably fast enough. I was hoping that
              something like '%n'%my_number already existed.

              Comment

              Working...