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

The Archive Base Latest Questions

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

I am maintaining a Python script that uses xlrd to retrieve values from Excel

  • 0

I am maintaining a Python script that uses xlrd to retrieve values from Excel spreadsheets, and then do various things with them. Some of the cells in the spreadsheet are high-precision numbers, and they must remain as such. When retrieving the values of one of these cells, xlrd gives me a float such as 0.38288746115497402.

However, I need to get this value into a string later on in the code. Doing either str(value) or unicode(value) will return something like “0.382887461155”. The requirements say that this is not acceptable; the precision needs to be preserved.

I’ve tried a couple things so far to no success. The first was using a string formatting thingy:

data = "%.40s" % (value) 
data2 = "%.40r" % (value) 

But both produce the same rounded number, “0.382887461155”.

Upon searching around for people with similar problems on SO and elsewhere on the internet, a common suggestion was to use the Decimal class. But I can’t change the way the data is given to me (unless somebody knows of a secret way to make xlrd return Decimals). And when I try to do this:

data = Decimal(value)

I get a TypeError: Cannot convert float to Decimal. First convert the float to a string. But obviously I can’t convert it to a string, or else I will lose the precision.

So yeah, I’m open to any suggestions — even really gross/hacky ones if necessary. I’m not terribly experienced with Python (more of a Java/C# guy myself) so feel free to correct me if I’ve got some kind of fundamental misunderstanding here.

EDIT: Just thought I would add that I am using Python 2.6.4. I don’t think there are any formal requirements stopping me from changing versions; it just has to not mess up any of the other code.

  • 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-16T05:48:42+00:00Added an answer on May 16, 2026 at 5:48 am

    I’m the author of xlrd. There is so much confusion in other answers and comments to rebut in comments so I’m doing it in an answer.

    @katriealex: “””precision being lost in the guts of xlrd””” — entirely unfounded and untrue. xlrd reproduces exactly the 64-bit float that’s stored in the XLS file.

    @katriealex: “””It may be possible to modify your local xlrd installation to change the float cast””” — I don’t know why you would want to do this; you don’t lose any precision by floating a 16-bit integer!!! In any case that code is used only when reading Excel 2.X files (which had an INTEGER-type cell record). The OP gives no indication that he is reading such ancient files.

    @jloubert: You must be mistaken. "%.40r" % a_float is just a baroque way of getting the same answer as repr(a_float).

    @EVERYBODY: You don’t need to convert a float to a decimal to preserve the precision. The whole point of the repr() function is that the following is guaranteed:

    float(repr(a_float)) == a_float
    

    Python 2.X (X <= 6) repr gives a constant 17 decimal digits of precision, as that is guaranteed to reproduce the original value. Later Pythons (2.7, 3.1) give the minimal number of decimal digits that will reproduce the original value.

    Python 2.6.4 (r264:75708, Oct 26 2009, 08:23:19) [MSC v.1500 32 bit (Intel)] on win32
    >>> f = 0.38288746115497402
    >>> repr(f)
    '0.38288746115497402'
    >>> float(repr(f)) == f
    True
    
    Python 2.7 (r27:82525, Jul  4 2010, 09:01:59) [MSC v.1500 32 bit (Intel)] on win32
    >>> f = 0.38288746115497402
    >>> repr(f)
    '0.382887461154974'
    >>> float(repr(f)) == f
    True
    

    So the bottom line is that if you want a string that preserves all the precision of a float object, use preserved = repr(the_float_object) … recover the value later by float(preserved). It’s that simple. No need for the decimal module.

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

Sidebar

Ask A Question

Stats

  • Questions 499k
  • Answers 499k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer I figured it out. Not sure why I was using… May 16, 2026 at 12:41 pm
  • Editorial Team
    Editorial Team added an answer Not sure if you want to use Javascript. If you… May 16, 2026 at 12:41 pm
  • Editorial Team
    Editorial Team added an answer It might be your shell file association that does not… May 16, 2026 at 12:41 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Related Questions

Say you're on a team that's maintaining a lot of internal python libraries(eggs), and
I'm in the process of writing a python script, and I want to find
I am maintaining a Python program, and am struggling to understand the relationships between
I wrote a quick program in python to add a gtk GUI to a
We have a number of customers that we have to send monthly invoices too.
Do the following on the default Python install on Mac OS X 10.5 (Leopard)
I am currently maintaining some Wordpress MU installations ( v2.9.x ) I want to
Quite new to functional languages, but I'm maintaining someone else's code with a lot
I have many, (15-20) different XML files that I need to load to VB.Net.
I'm currently in the middle of porting a fairly large Perl The problem is

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.