Sponsored Links
Sponsored Link

sponsored links

Collapse

Announcement

Collapse
No announcement yet.

Rounding Amount To The Nearest 25p

Collapse
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Rounding Amount To The Nearest 25p

    I need to do a calculation that takes currency amounts and rounds to the nearest 10p, 25p (could be other values - file driven).
    I can't see an obvious way to do this using RPG Free Format, although maybe I need to use a DB2 SQL function (if there is one).
    Wondered if anyone had to do something similar or had any ideas on how to achieve this in RPG Free Format.

    One reason why we need to do this is to create prices for Ireland, where the smallest coin is 5c, so every price must be in multiples of 0.05.
    We are also looking to create sales prices that are rounded, based on a formula, hence the 10p/25p.

    Thanks in advance for any ideas.
    I know I can do this the "hard way", but hoping there is a function that will make life simpler.
    Poddys Rambles On

  • #2
    I _think _ this will work but I haven't time to test it. I'll lay it out in individual steps and leave you to combine as appropriate.

    dcl-s value packed(7:2);
    dcl-s cents int(10);
    dcl-s units int(10);
    dcl-s roundingUnit int(5);
    dcl-s roundedValue packed(7:2);

    cents = value * 100;
    eval units(h) = cents / roundingUnit;
    roundedValue = units * roundingUnit / 100;

    So with a start value of 7.87 and rounding to the nearest .10 ...

    cents = 787
    units = 79
    roundedValue = 7.90



    Comment


    • #3
      P.S. I'm surprised that you are changing prices for Ireland like this. In Canada we also dd away with the penny but the calculation is done at the point of sale. reason being that if I have an 8 cent item, raising its price to 10 cents is a huge increase - particularly is someone buys 10 of them! So the price is kept at 8 cents and the total rounded to the nearest 5 cents. In the case of credit card charges the original 8 cents would be charged.

      Comment


      • Poddys
        Poddys commented
        Editing a comment
        Point taken, but I am just the person tasked with doing this, not the person who makes the decisions.
        We are a wholesaler not a retailer, it's really for calculating "Typical Selling Prices" for tools, so our retail customers can still charge what they want.
        The company wants the calculated TSP's to be rounded to the nearest 10 or 25 depending on the price, but it could in theory mean rounding to any value, hence the desire to try and create a generic formula.

    • #4
      There is no SQL function for rounding to 5, 10 or whatever position. You need to multiply and devide.
      Example: The following SQL example rounds commercially to 5 cent

      Code:
      Round(YourValue * 2.00 , 1) / 2.00 , 11 , 4)
      Birgitta
      Last edited by B.Hauser; 1 week ago.

      Comment


      • Poddys
        Poddys commented
        Editing a comment
        Thanks Birgitta, and to everyone else who responded.
        I will try the suggested solutions.
        Appreciate the help.

    • #5
      I found a Java formula which is the same as yours Birgitta.
      I tested it using Windows Calculator, and it works fine.
      Now to apply this in RPG Free.

      Code:
      double rounded = Math.round(num * 100.0 / 5.0) * 5.0 / 100.0;
      Poddys Rambles On

      Comment


      • #6
        I have used the same formula that JonBoy used years ago.

        I find that it is clearer and easier to document when you have something like this
        RoundingUnit = 0.05;
        RoundedAmount = %inth(Amount / RoundingUnit) * RoundingUnit;

        When you have to change the rounding unit to 0.10 or 0.25 you just have to replace the 0.05 with that value.

        Peder

        Comment


        • #7
          The only thing you have to watch out for when doing it all in a single calc is the the increased accuracy could (in theory) result in a different answer to that expected due to the intermediate results using floating point.

          If you use a multi-step approach you control the accuracy at each step.

          Probably not important in this calculation but it can have an impact.

          Comment


          • #8
            This is my finished procedure. I'm sure it could be improved on, but the formula works ok.
            Thanks for your help everyone.

            Code:
            dcl-proc roundPrice;
            
                     dcl-pi roundPrice   zoned(7:2);
                       unroundedPrice      zoned(7:2) const;
                       rounding            zoned(3:2) const;
                     end-pi;
            
                     dcl-s  roundingX100   int(5);
                     dcl-s  roundedPrice   zoned(7:2);
            
                     roundingX100 = rounding * 100;
            
                     roundedPrice = %dech(unroundedPrice * 100 / roundingX100 : 15 : 0)
                                    * roundingX100 / 100;
            
                     return roundedPrice;
            
                   end-proc;
            Poddys Rambles On

            Comment

            sponsored links

            Collapse

            Working...
            X