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

  • SEARCH
  • Home
  • 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 8053459
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T07:51:04+00:00 2026-06-05T07:51:04+00:00

Similar posts such as the following do not answer my question. Convert a string

  • 0

Similar posts such as the following do not answer my question.
Convert a string to integer with decimal in Python

Consider the following Python code.

>>> import decimal
>>> s = '23.456'
>>> d = decimal.Decimal(s)
>>> d
Decimal('23.456')           # How do I represent this as simply 23.456?
>>> d - 1
22                          # How do I obtain the output to be 22.456?

How do I convert a string to a decimal number, so I am able to perform arithmetic functions on it and obtain an output with the correct precision?

  • 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-05T07:51:05+00:00Added an answer on June 5, 2026 at 7:51 am

    If you want to stay in decimal numbers, safest is to convert everything:

    >>> s = '23.456'
    >>> d = decimal.Decimal(s)
    
    >>> d - decimal.Decimal('1')
    Decimal('22.456')
    >>> d - decimal.Decimal('1.0')
    Decimal('22.456')
    

    In Python 2.7, there’s an implicit conversion for integers, but not floats.

    >>> d - 1
    Decimal('22.456')
    >>> d - 1.0
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    TypeError: unsupported operand type(s) for -: 'Decimal' and 'float'
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've read multiple similar posts on this, and my code seems to match the
I've seen a few posts about different solutions such as Sandcastle for documenting code.
I realize there are similar posts to this out there, but rest assured, this
i have seen similar posts here but i have had no luck solving my
I have found many similar posts and even tried to find out how to
I have already read all the previous similar posts but I couldn't find a
ok, i found some similar posts on stackoverflow, but I couldn't find any that
I've read Get File Icon used by Shell and the other similar posts -
Curious what "Toast" means? Saw this and am curious... Similar Posts How to add
I was looking in posts similar to mine, but didn't find any response to

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.