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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T01:56:17+00:00 2026-06-16T01:56:17+00:00

For example, the distance is defined by meters. Procedure should format value on a

  • 0

For example, the distance is defined by meters.
Procedure should format value on a scale:

if distance < 100:
    return '%dm' % distance
elif distance < 1000:
    return '%.0fm' % round(distance, -1)
else:
    import math
    m, km = math.modf(distance / 1000.0)
    if m < 0.01:
        return '%.0fkm' % km
    else:
        return '%.1fkm' % km + round(m, 1)

It would be great to use already existing solution.

Update: django-distanceformatting is launched, but the question remains open.

  • 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-16T01:56:18+00:00Added an answer on June 16, 2026 at 1:56 am

    There are some generic unit handling modules for Python, which could be wrapped in a template tag (no specific order):

    • quantities
    • Buckingham
    • units
    • Unum

    Maybe one of them gives you what you need. But I still stand to my earlier comment that if you only need so little functionality, you should not need to bother about reinventing the wheel (or your tiny part thereof).

    After a closer look at your code, I found some inconsistencies:

    • 1.99999 is returned as 1m (should probably be 2m)
    • I assume that you want to keep two significant digits, but 1.2 is returned as 1m or 0.12 as 0m
    • The last line needs parentheses around the values tuple

    Here is a more consistent and concise version of your function (plus a little wrapping around to test):

    from math import modf
    def old_dist_format(distance):
        if distance < 100:
            return '%dm' % distance
        elif distance < 1000:
            return '%.0fm' % round(distance, -1)
        else:
            m, km = modf(distance / 1000.0)
            if m < 0.01:
                return '%.0fkm' % km
            else:
                return '%.1fkm' % (km + round(m, 1))
    
    
    def dist_format(distance):
        rounded = float('%.2g' % distance)
        km = modf(rounded / 1000.0)
        if km[1]:
            return "%gkm" % (km[0] + km[1])
        else:
            return "%gm" % rounded
    
    
    if __name__ == "__main__":
        while True:
            try:
                s = raw_input("> ")
                try:
                    d = float(s)
                except ValueError:
                    print "Enter number"
                    continue
                print "%s\t%s" % (dist_format(d), old_dist_format(d))
            except KeyboardInterrupt:
                print
                break
    

    The first line in the new dist_format() function always keeps two significant figures. %g leaves out everything from the decimal point if not needed. Caveat: My function will give you exponential notation for (astronomic) distances >= 1000000000.

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

Sidebar

Related Questions

I am playing with the euclidian distance example from programming collective intelligence book, #
I am plotting speed/distance data from racing events, using Matplotlib, for example distance vs.
Here's the textbook example of the general algorithm to calculate Levenshtein Distance (I've pulled
I am using following example source code but I can't get long distance route
Example Div: <div class=container> <div class=select1></div> <div class=select2></div> <div class=select3></div> </div> Now I want
Example: print max(chain_length(i) for i in xrange(1,10001)) This returns the maximum/biggest chain_length (an arbitrary
Example: $ objdump Logger.cpp.o -t 00000000 g F .text 00000000 .hidden __sti___10_Logger_cpp_0b2ae32b
Example: copy in one tmux pane (via vim), then switch to another pane (running
Example: an Employee table with an optional DateOfBirth field can be normalized into two
Example CSS #wrap{margin:20px} Code prettify wraps the whole line in .com <span class=com>#wrap{margin:20px}</span> Somebody

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.