Re: Need help rounding off my dollar amount

From: René Allan Larsen (rene.larsen_at_spamfilter.dk)
Date: 11/06/03

  • Next message: Marc Pelletier: "dmActiveForm in MDI application"
    Date: Thu, 06 Nov 2003 19:45:20 +0100
    
    

    In article <qktjqv0md2ddv58d4hqq3dl53887shqmjs@4ax.com>, Pat Mc wrote:
    >
    > Basically, I want a function to round off my dollar value. Sounded
    > simple enough when I first started it !!
    >
    > Examples would be
    > 1.22 goes to 1.20
    > 1.23 goes to 1.25
    > 1.98 goes to 2.00
    >
    > In my initial attempt, I passed a currency amount to my function and
    > had ideas of isolating the cents, tens of cents and dollar values. It
    > became messy in the type conversions between currency, string and
    > floating - I had string amounts of 123.2450 instead of nice 123.25.
    >
    > Is there a standard Currency 'Rounding' function 'out there' where I
    > can pass a value to it and it returns a value to 2 decimal places? or
    > can someone offer advice or some rules to go by.

    The algorithm is simple:
        Divide value by 0.05, as you want to round to nearest 0.05
        Add 0.5 to value
        Truncate decimals
        Multiply value by 0.05

    Here is a worked example:
    > 1.22 goes to 1.20
        Divide: 1.22 / 0.05 -> 24.40
        Add: 24.40 + 0.5 -> 24.90
        Trunc: 24.90 -> 24
        Multiply: 24 * 0.05 -> 1.20

    > 1.23 goes to 1.25
        Divide: 1.23 / 0.05 -> 24.60
        Add: 24.60 + 0.5 -> 25.10
        Trunc: 25.10 -> 25
        Multiply: 25 * 0.05 -> 1.25

    Now this may *not* give you a correct answer because of the fact, that
    floating point calculations are imprecise on a PC.

    Try looking for this post in the archives:
    From: "John Herbster \(TeamB\)" <herb-sci at novus-tele.net>
    Newsgroups: borland.public.attachments
    Subject: T_Currency_1 -- test of dollar and cents rounding.
    Date: Thu, 28 Nov 2002 09:51:11 -0800

    And this one too:
    From: "John Herbster" <johnh@petronworld.com>
    Newsgroups: borland.public.attachments
    Subject: Unit for doing best possible decimal rounding.
    Date: Tue, 30 Apr 2002 13:44:13 -0500

    Regards, René


  • Next message: Marc Pelletier: "dmActiveForm in MDI application"

    Relevant Pages

    • Re: How to set a value to 0 decimal after calculation in query?
      ... decimals), but the number is still 12.3546. ... There are 2 methods to round the computation. ... >> Change the Format property on the property sheet as required. ... >> Change the Decimal Places property on the property sheet as required. ...
      (microsoft.public.access.queries)
    • Re: Rounding error when converting from double to int
      ... interest calculations). ... We want to round any fractional cents up to ... We use a rounding scheme where values greater than ...
      (comp.lang.c)
    • Re: Number formatting
      ... To just convert to no decimals, you can convert the result to an integer: ... This will round down regardless of the decimal, which may not be what you ... For display purposes, Access VBA allows you do do a lot of this by simply ... >>I have created a form and set the format of the fields to ...
      (microsoft.public.access.forms)
    • Re: How do I use VBS in a form?
      ... > calculations within the contract based on the specifics of the job. ... > We offer a 3% discount for speedy payments, ... "I want numbers with decimals to round up" ...
      (microsoft.public.word.vba.general)
    • Re: floating point display problem
      ... public class PrintTest { ... Floating point numbers aren't exact. ... far from guaranteed that a 7 decimals number can be represented ... different from x to also round to y. ...
      (comp.lang.java.programmer)