Advertisement

07.11.2008 at 03:00PM PDT, ID: 23558794
[x]
Attachment Details
[x]
The Solution Rating System

With so many solutions, how can you tell which solutions are most likely to help you and which ones are not? To provide you with a tool to use, we rate our solutions based on various elements that most accurately determine if a solution is a quality solution. To explain what factors affect the solution rating, here are the elements we take into consideration when formulating our solution rating.

  • The Grade of the Solution
  • The Zone Rank of the Expert Providing the Solution
  • The Number of Author and Expert Comments
  • The Number of Experts Contributing
  • The Feedback of the Community

Your Input Matters
Because of the way the system is set up, the most important variable in this equation is you. As a member of Experts Exchange, you are able to cast your vote on the quality of the solutions in regard to how complete, accurate, helpful and easy to understand each solution is. When you provide your feedback, each rating is adjusted accordingly. So, if you see a solution that has a poor rating that you think is a good solution, let us know by rating it. As you do, the rating will be adjusted and will become more accurate for other members of our site.

If you have any suggestions that you would like to make for our rating system, please ask a question in the Suggestions Zone of Community Support.

Thank you!

8.2

How can I add leading and following zero's to a floating point number?

Asked by mitchguy in Python Scripting Language

Tags:

I'm trying to display a string in the format 000.000 which will represent a calculated number
in my program. The calculated numbers will not always have 3 digits on each side of the decimal place.
for instance here are a couple of example numbers

1.22
22.34

So what I need to do is make a string from the number but the string needs to have left and right padding
of zeros, so the numbers above would be
001.220
022.340

I was trying to copy the numberString into the defaultString by position listed below, but
apparently Strings are immutable and I can't insert a substring. I don't know if there is a way
to add the padding to the number first before I make it a string or a way to do it as a string.
Either way I'm a little stumped since I've only been using Python a few weeks.


numberString = str(1.22)
decimalPosition = numberString.find('.')
length = len(numberString)
defaultString = "000.000"
startPosition = length - 1 - decimalPosition  #position in default string to start

        for s in range(0,decimalPosition,1):
            defaultString[startPosition] = numberString[s]
            startPosition = startPostion + 1

        for s in range(decimalPosition+1,length,1):
            defaultString[startPosition] = numberString[s]
            startPosition = startPostion + 1


Start Free Trial
[+][-]07.11.2008 at 03:24PM PDT, ID: 21986534

View this solution now by starting your 7-day free trial. Setting up your free trial is quick, easy, and secure. We will return you to this solution, unlocked, when you're done.

 

About this solution

Zone: Python Scripting Language
Tags: Python
Sign Up Now!
Solution Provided By: sprjp2
Participating Experts: 1
Solution Grade: A
 
 
 
Loading Advertisement...
20080716-EE-VQP-32 / EE_QW_2_20070628