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 958231
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T00:49:48+00:00 2026-05-16T00:49:48+00:00

I am writing a web2py application that requires summing dollar amounts without losing precision.

  • 0

I am writing a web2py application that requires summing dollar amounts without losing precision. I realize I need to use Decimals for this, but I’ve found myself having to wrap every single number I get from the database with:

Decimal(str(myval))

Before I go crazy adding that to all of my code, is there a better way? I’m new to Python, so it’s very possible that I am overlooking something obvious.

Edit: My database is MS SQL Server and I’m storing the amounts in SQL Server money fields (I believe the implementation is analogous to Decimal, ie integer math, not floating point).

I am connecting to the db through the web2py framework (which uses pyodbc for SQL Server connections). I believe web2py has some support for decimal types. For example, my web2py field definitions look like: Field('Amount','decimal(19,4)') However, when I return a value from the database using web2py’s .executesql method it returns the value as a float and not a Decimal.

Edit: This appears to be an issue with FreeTDS and MS SQL Server. As Massimo stated in the comments, web2py supports this properly and returns a Decimal (if it can). It turns out this is only an issue in my production environment (Linux). I am using the FreeTDS driver to connect to MS SQL and it appears to be translating the MS SQL money type to a python float.

I think Alex Martelli’s answer is pointing in the right direction. Does anyone have any experience with FreeTDS, MS SQL, and python? I think this probably warrants its own question, so I’ll move this discussion… (new question posted here: FreeTDS translating MS SQL money type to python float, not Decimal)

Update: There was in fact a bug in FreeTDS. This was fixed in the CVS head of FreeTDS as of August 4, 2010.

  • 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-05-16T00:49:49+00:00Added an answer on May 16, 2026 at 12:49 am

    First, keep all numbers in decimal form in the database — you don’t mention what DB engine you’re using, but every engine supports such functionality, e.g., here is MySQL’s DECIMAL type documentation. I hope you’re already doing that, but, if you aren’t, it’s still worth the pain of a schema change.

    Then, you need to ensure the type conversion to and from the DB matches the database’s decimals with Python’s. Unfortunately there is no standard way to do it across DB-API implementations, but most do offer a way; for example, with MySQLDB you need to pass a conv= parameter in connect, known as a “type converters dictionary” — the default is a copy of MySQLdb.converters.conversions but of course you can enrich it to do the right thing with decimals. If you tell us which DB you’re using, and what DB-API connector package you prefer for it, we may be able to give you more detailed help on this subject.

    Edit: @unutbu remarks in a comment that the current MySQLdb converter already does translate decimals correctly, so, unless you’re stuck with a very old release of MySQLdb, you should be fine as long as you do correctly keep all the numbers as decimal in the DB (yay!).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

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.