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

The Archive Base Latest Questions

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

I have a Django Queryset object called data containing string keys and float values.

  • 0

I have a Django Queryset object called data containing string keys and float values.

I want to convert all floats to 2 decimal places.

If I do this:

>>> param1_values = [d[param1] for d in data]
>>> param1_values[:5]
[26.1, 24.6, 26.2444444444444, 27.3103448275862, 26.7576923076923]

…which is fine but I need to float to two decimal places.

However, if I do this:

>>> param1_values = ["%.2f" % d['mean_air_temp'] for d in data]
>>> param1_values[:5]
['26.10', '24.60', '26.24', '27.31', '26.76']

The numbers are what I wanted but they are converted to strings! Why does this occur and how do I solve it?

Any help appreciated.

  • 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:53+00:00Added an answer on June 1, 2026 at 5:26 am

    You can use the built-in round function:

    param1_values = [round(d[param1], 2) for d in data]
    

    Note: The behavior of round() for floats can be surprising: for
    example, round(2.675, 2) gives 2.67 instead of the expected 2.68. This
    is not a bug: it’s a result of the fact that most decimal fractions
    can’t be represented exactly as a float. See Floating Point
    Arithmetic: Issues and Limitations
    for more information.

    This seems to be accurate enough for your use case. If you need a more precise answer see Ignacio’s response using Decimal.

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

Sidebar

Related Questions

I have a Django QuerySet , and I want to get a Q object
I think I don't unterstand django-haystack properly: I have a data model containing several
I need to re-order a Django queryset, because I want to put None values
I have a django model with a DateTimeField called when which I want to
I have a list of objects from a django queryset, e.g. my_list = MyObject.objects.filter(variable=something)
I have a question about django content_types In the example of filtering a QuerySet
I have django model which consist of parent child relationship filed I want to
i have django installed and seems to be working it worked page I want
I have a list of object IDs coming out of Redis and I want
New to Django so it's probable I'm doing this all wrong. I have an

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.