IssacJones
asked on
decimal places in a string
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
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
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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_st ring]
a = 17.5678
length_of_final_string = len('%.*f' % (number_of_decimal_places,
b = str(a)[:length_of_final_st
ASKER