Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8487695
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T21:19:15+00:00 2026-06-10T21:19:15+00:00

I am trying to print a number into engineering format with python, but I

  • 0

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

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T21:19:17+00:00Added an answer on June 10, 2026 at 9:19 pm

    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.

    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
    

    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.


    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):

    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:

    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).

    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.

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to convert a single precision floating point number into an ascii representation
I'm trying to print a number of '+' characters followed by a number of
I'm trying to print in the screen this, given any number like 12: 0+12
I'm trying to insert new records into SQLite database from Python code. con =
I am trying to grab the version number from a string via python regex...
So I am trying to do something simple: printfn Enter a number: try let
I'm trying to get the body of an email into a string to do
I'm trying to code a program that will print out a certain number of
First off, this is homework. I'm trying to read a 5 digit number into
I am trying to print users personal data like name, email, phone number on

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.