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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T11:35:17+00:00 2026-06-13T11:35:17+00:00

I’ve got a system that handles account & COD transactions. When a client is

  • 0

I’ve got a system that handles account & COD transactions. When a client is paying via COD, I need to round up or round down the cents.

Eg.
Total = 64.42 - rounds to: 64.40
Total = 64.57 - rounds to 64.60

I’m trying to figure out the best way to do this in python and also store the difference in a decimal field in a database.

I’m using DJANGO. I was trying to use the ROUND_05UP part of decimal but I’m not sure how to do it. I wrote a custom filter to display it, but that’s not even working:

@register.filter
def currency(value, arg='en_US', symbol=True):

    saved = '.'.join([x for x in locale.getlocale() if x]) or (None, None)
    given = arg and ('.' in arg and str(arg) or str(arg) + '.UTF-8')

    # Workaround for Python bug 1699853 and other possibly related bugs.
    if '.' in saved and saved.split('.')[1].lower() in ('utf', 'utf8'):
        saved = saved.split('.')[0] + '.UTF-8'

    if saved == (None, None) and given == '':
        given = 'en_US.UTF-8'

    try:
        locale.setlocale(locale.LC_ALL, given)

        return locale.currency(value or 0, symbol, True)

    except (TypeError, locale.Error):
        return ''

    finally:
        locale.setlocale(locale.LC_ALL, saved)


@register.filter
def currency_cash(value):
    value = decimal.Decimal(value)
    return currency(value.quantize(Decimal(10) ** -2, rounding=decimal.ROUND_05UP))

Does anyone have a function written that does this?

UPDATE: The reason for this in australia we have no coins less then 5 cents. So 45.55 would stay as 45.55.

Cheers,
Ben

  • 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-13T11:35:18+00:00Added an answer on June 13, 2026 at 11:35 am

    If I understand correctly, the incoming amount is already an exact integer number of cents, and you just want to round to the nearest multiple of 10 cents, but leaving values that already end in 5 as they are. Is that correct? Here’s a simple solution using the remainder_near method.

    from decimal import Decimal
    
    def round_to_10_cents(x):
        """
        Round a Decimal value to the nearest multiple of 0.10,
        unless already an exact multiple of 0.05.
    
        """
        remainder = x.remainder_near(Decimal('0.10'))
        if abs(remainder) == Decimal('0.05'):
            return x
        else:
            return x - remainder
    
    
    # Test code.
    for x in range(80, 120):
        y = Decimal(x) / Decimal('1E2')
        print "{0} rounds to {1}".format(y, round_to_10_cents(y))
    

    Sample output:

    0.80 rounds to 0.80
    0.81 rounds to 0.80
    0.82 rounds to 0.80
    0.83 rounds to 0.80
    0.84 rounds to 0.80
    0.85 rounds to 0.85
    0.86 rounds to 0.90
    0.87 rounds to 0.90
    0.88 rounds to 0.90
    0.89 rounds to 0.90
    0.90 rounds to 0.90
    0.91 rounds to 0.90
    0.92 rounds to 0.90
    0.93 rounds to 0.90
    0.94 rounds to 0.90
    0.95 rounds to 0.95
    0.96 rounds to 1.00
    0.97 rounds to 1.00
    0.98 rounds to 1.00
    0.99 rounds to 1.00
    1.00 rounds to 1.00
    1.01 rounds to 1.00
    1.02 rounds to 1.00
    1.03 rounds to 1.00
    1.04 rounds to 1.00
    1.05 rounds to 1.05
    1.06 rounds to 1.10
    1.07 rounds to 1.10
    1.08 rounds to 1.10
    1.09 rounds to 1.10
    1.10 rounds to 1.10
    1.11 rounds to 1.10
    1.12 rounds to 1.10
    1.13 rounds to 1.10
    1.14 rounds to 1.10
    1.15 rounds to 1.15
    1.16 rounds to 1.20
    1.17 rounds to 1.20
    1.18 rounds to 1.20
    1.19 rounds to 1.20
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I've got a string that has curly quotes in it. I'd like to replace
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I need a function that will clean a strings' special characters. I do NOT
I'm working with an upstream system that sometimes sends me text destined for HTML/XML
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have a French site that I want to parse, but am running into
In my XML file chapters tag has more chapter tag.i need to display chapters
I am doing a simple coin flipping experiment for class that involves flipping a

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.