Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7727339
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T05:26:06+00:00 2026-06-01T05:26:06+00:00

I am experiencing a problem when trying to use the getcontext().prec = 2 function,

  • 0

I am experiencing a problem when trying to use the getcontext().prec = 2 function, trying to get decimal results with only 2 decimals. I have experimented with several variations, but don’t know why I can’t transform float to string inside one of my lists. I am new using Python (2.6), and have not been able to find a solution for this. Thanks in advance for any help.

from __future__ import division
from decimal import *

getcontext().prec = 2

list_a = ['abc','def','ghi']
list_b = [123, 534, 345]
list_c = [Decimal(str(1/6)), Decimal(str(1/1234)), Decimal(str(2/9))]

for r,c,p in zip(list_a, list_b, list_c):
  print '{0} goes with {1} and with {2}%.'.format(r,c,p)

Returns

SyntaxError: Invalid syntax

If I eliminate the str() from the print line, the results are printed, but w/o constraints in the number of decimals. If I use Decimal() – w/o using str first, I get

'first convert the float to a string'. 

Any insights?
Cheers

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T05:26:08+00:00Added an answer on June 1, 2026 at 5:26 am
    Decimal(str((2/9))
    

    should be

    Decimal(str(2/9))
    

    You have an unclosed ) in the code shown in the example.

    To display only two decimal places:

    from __future__ import division
    from decimal import *
    
    getcontext().prec = 2
    
    list_a = ['abc','def','ghi']
    list_b = [123, 534, 345]
    list_c = [Decimal(str(1/6)), Decimal(str(1/1234)), Decimal(str(2/9))]
    for r,c,p in zip(list_a, list_b, list_c):
      print '{0} goes with {1} and with {2:.2f}%.'.format(r,c,p)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to use ProGuard to obfuscate a Java application. I'm experiencing a problem
I'm experiencing a serious problem when I'm trying to use the Memcached caching driver
I'm experiencing a strange problem while trying to use the Ajax.BeginForm method of ASP.NET
I'm experiencing a problem while trying to use google maps api v3 (with marker
After installing rails3, I'm experiencing problems when trying to use haml with it. I
I'm experiencing a strange problem when trying to count the entities in a managed
I have a Windows Server 2003 64-Bit VPS, and I'm experiencing a problem with
I have been trying to understand function pointers in C++ so that I can
I'm trying to use jsonPath and the pick function to determine if a rule
i'm experiencing problems while trying to use the object currentActeurObjetProjet and display its attributes

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.