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 5931401
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T14:41:21+00:00 2026-05-22T14:41:21+00:00

My goal is simply to convert a string such as 1.2 to scientific notation

  • 0

My goal is simply to convert a string such as “1.2” to scientific notation without adding additional precision. The problem is that I always end up with superfluous 0s at the end of my output.

>>> input = "1.2"
>>> print '{:e}'.format(float(input))
1.200000e+00

I’m trying to figure out how to get just 1.2e+00. I realize I can specify precision in my format statement, but I don’t want to truncate longer strings unnecessarily. I just want to suppress the training 0s.

I’ve tried using Decimal.normalize(), which works in all cases, except where e < 2.

>>> print Decimal("1.2000e+4").normalize()
1.2E+4
>>> print Decimal("1.2000e+1").normalize()
12

So that’s better, except I don’t want 12, I want 1.2e+1. 😛

Any suggestions would be greatly appreciated!

Edit:
To clarify, the input value has already been rounded appropriately to a predetermined length that is now unknown. I’m trying to avoid recalculating the appropriate formatting precision.

Basically, I could have input values of “1.23” and “1234.56”, which should come out as “1.23e+0” and “1.23456e+3”.

I may have to just check how long the input string is and use that to specify a precision manually, but I wanted to check and make sure I just wasn’t missing something that could just prevent the exponential format from arbitrarily adding 0s.

  • 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-05-22T14:41:22+00:00Added an answer on May 22, 2026 at 2:41 pm

    Just going back through and cleaning up old questions. I ended up solving this by writing a little function to intuit the initial precision of a number and then using it to format the output result.

    #used to determine number of precise digits in a string
    def get_precision(str_value):
        vals =  str_value.split('.')
        if (vals[0] == '0'):
            return len(vals[1])
        else:
            return len(str_value) -1
    
    # maintain same precision of incoming string on output text
    class ExpDecorator(CurrencyDecorator):
        def get_text(self):
            text = self.decoratedCurrency.get_text()
            return ('{:.' + str(get_precision(text)-1) + 'e}').format(float(text))
    

    Not really the most elegant solution, but the task was kind of obnoxious to begin with and it got the job done.

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

Sidebar

Related Questions

Is there a Maven phase or goal to simply execute the main method of
goal: I have the string 1234432144 I want to only replace the first 2
I've come up with a simple example that hopefully illustrates my goal. I currently
Goal: I need to be able to convert apostrophes to properly formed words. -
My goal is to simply use a pop-up box to ask the user for
My goal is to simply update the lastmod node in a simple sitemap xml
My goal is to simply have the cursor swap to be a hand (pointer)
My goal is to build a standalone RESTful Rails 3 service that communicates with
I have a high-level goal of creating a static utility class that encapsulates the
I have following goal: From a list in main activity that extends ListActivity, I

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.