Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

decimal places in a string

Posted on 2011-09-08
4
Medium Priority
?
512 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
  • 2
4 Comments
 
LVL 6

Assisted Solution

by:LeeeRussell
LeeeRussell earned 80 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 920 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: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say 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

"The time has come," the Walrus said, "To talk of many things: Of sets--and lists--and dictionaries-- Of variable kinks-- And why you see it changing not-- And why so strange are strings." This part describes how variables and references (see …
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…
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
Course of the Month13 days, 12 hours left to enroll

963 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