Solved

decimal places in a string

Posted on 2011-09-08
4
467 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 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 28

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Help with my python script 6 154
Python negative lookahead for aaa:bbb@hostname 6 92
How Do You Use MacPorts & Python To Create A Program? 22 71
How can I do this in Pyhton? 12 109
The purpose of this article is to demonstrate how we can upgrade Python from version 2.7.6 to Python 2.7.10 on the Linux Mint operating system. I am using an Oracle Virtual Box where I have installed Linux Mint operating system version 17.2. Once yo…
Article by: Swadhin
Introduction of Lists in Python: There are six built-in types of sequences. Lists and tuples are the most common one. In this article we will see how to use Lists in python and how we can utilize it while doing our own program. In general we can al…
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 lists in Python. Lists, as their name suggests, are a means for ordering and storing values. : Lists are declared using brackets; for example: t = [1, 2, 3]: Lists may contain a mix of data types; for example: t = ['string', 1, T…

930 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now