Solved

decimal places in a string

Posted on 2011-09-08
4
503 Views
Last Modified: 2012-05-12
Hi

I have a decimal value which I have calculated. I want to convert this to a string but only to a certain precision e.g. 2 decimal places. Can anybody tell me how to do this?

Issac
0
Comment
Question by:IssacJones
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 6

Assisted Solution

by:LeeeRussell
LeeeRussell earned 20 total points
ID: 36502271
To turn a decimal value to a string use the str() function:

a = 17.5678
b = str(a)

If you want to have it to a certain precision (with rounding) use the round() function:

c = round(a,2)
d = str(c)

Or combine it all:

Rounded_String = str(round(17.5678,2))
0
 
LVL 29

Accepted Solution

by:
pepr earned 230 total points
ID: 36502500
You probably do not want to round the value only for printing.  Also, it may be unreliable.

What you want is what is called a string interpolation or formatting string.  You can use or old way (resembling the C printf) or new way (resembling the C# way). Try the following in the interactive Python:

>>> x = 17.5678
>>> s = '%.2f' % x
>>> s
'17.57'
>>> s2 = '{:.2f}'.format(x)
>>> s2
'17.57'

Open in new window

0
 

Author Closing Comment

by:IssacJones
ID: 36502776
Many thanks
0
 
LVL 6

Expert Comment

by:LeeeRussell
ID: 36502808
The method presented by pepr does round the result.  If you truly want to truncate the result, I think you'd have to do something like:

a = 17.5678

length_of_final_string = len('%.*f' % (number_of_decimal_places, a))

b = str(a)[:length_of_final_string]
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Installing Python 2.7.3 version on Windows operating system For installing Python first we need to download Python's latest version from URL" www.python.org " You can also get information on Python scripting language from the above mentioned we…
Strings in Python are the set of characters that, once defined, cannot be changed by any other method like replace. Even if we use the replace method it still does not modify the original string that we use, but just copies the string and then modif…
Learn the basics of strings in Python: declaration, operations, indices, and slicing. Strings are declared with quotations; for example: s = "string": Strings are immutable.: Strings may be concatenated or multiplied using the addition and multiplic…
Learn the basics of if, else, and elif statements in Python 2.7. Use "if" statements to test a specified condition.: The structure of an if statement is as follows: (CODE) Use "else" statements to allow the execution of an alternative, if the …
Suggested Courses

627 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question