How to get the outcome of this to show the total plus 5%

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • rossdpk
    New Member
    • Jul 2010
    • 7

    How to get the outcome of this to show the total plus 5%

    Hi. I'm trying to get this script to show the total with 5% added on (tax).
    The code i have here gets the total of the volume multiplied by the price but i can't seem to figure out how to then add 5% onto this total.....

    Code:
    var unitvalue=0
    
    var dailyprice=0
    
    function calculate(){
    var litres=document.price.value
    var selectunit=document.price.units.options[document.price.units.selectedIndex].value
    
    if (selectunit=="300")
    unitvalue=selectunit*0.5000
    else if (selectunit=="500")
    unitvalue=selectunit*0.4100
    else if (selectunit=="600")
    unitvalue=selectunit*0.4100
    else if (selectunit=="700")
    unitvalue=selectunit*0.4100
    else if (selectunit=="900")
    unitvalue=selectunit*0.4100
    else if (selectunit=="1000")
    unitvalue=selectunit*0.4100
    else if (selectunit=="1200")
    unitvalue=selectunit*0.4100
    else if (selectunit=="1300")
    unitvalue=selectunit*0.4100
    else if (selectunit=="1800")
    unitvalue=selectunit*0.4100
    else if (selectunit=="2000")
    unitvalue=selectunit*0.4050
    
    
    alert (selectunit+" litres, including VAT, will cost:\n\n \u00A3"+unitvalue+"\n")
    }
    Any ideas anyone?
    Thanks in advance,
    Ross.
    Last edited by Dormilich; Jul 23 '10, 05:32 AM. Reason: Please use [code] tags when posting code
  • acoder
    Recognized Expert MVP
    • Nov 2006
    • 16032

    #2
    Adding 5% would be the same as multiplying by 1.05.

    PS. if only VAT were indeed 5%! ;)

    Comment

    • rossdpk
      New Member
      • Jul 2010
      • 7

      #3
      Originally posted by acoder
      Adding 5% would be the same as multiplying by 1.05.

      PS. if only VAT were indeed 5%! ;)
      Thanks for your reply!
      Any idea where in the code would I need to put the multiplication?

      Comment

      • rossdpk
        New Member
        • Jul 2010
        • 7

        #4
        Originally posted by rossdpk
        Thanks for your reply!
        Any idea where in the code would I need to put the multiplication?
        Ah, ok. I got it working now. As expected, it was quite a simple problem to solve. But man, thats had me frustrated for ages!
        Now to get it to round up to 2 decimels!!!
        Thanks acoder!

        Comment

        • rossdpk
          New Member
          • Jul 2010
          • 7

          #5
          Actually, could anyone help with getting this to round the total up to 2 decimal places?

          I know i'm a bit rubbish at this but i'm more a designer than developer!

          Thanks.

          Comment

          • JKing
            Recognized Expert Top Contributor
            • Jun 2007
            • 1206

            #6
            Code:
            var total;
            
            total = 25.6349;
            
            total = total.toFixed(2);

            Comment

            • rossdpk
              New Member
              • Jul 2010
              • 7

              #7
              Originally posted by JKing
              Code:
              var total;
              
              total = 25.6349;
              
              total = total.toFixed(2);
              I've been trying similar things to that but how and where does that fit into my code?

              Comment

              • JKing
                Recognized Expert Top Contributor
                • Jun 2007
                • 1206

                #8
                You want to round it after all calculations are finished.
                Code:
                total = total.toFixed(2);
                Place that after all calculations are done and replace total with the name of your variable.

                Comment

                • rossdpk
                  New Member
                  • Jul 2010
                  • 7

                  #9
                  Originally posted by JKing
                  You want to round it after all calculations are finished.
                  Code:
                  total = total.toFixed(2);
                  Place that after all calculations are done and replace total with the name of your variable.
                  Hi, i've tried putting [unitvalue = unitvalue.toFix ed(2);] after the calculations, and in different parts of the code but it still wont give the total as 2 decimel places. Any ideas?

                  Comment

                  • rossdpk
                    New Member
                    • Jul 2010
                    • 7

                    #10
                    Originally posted by rossdpk
                    Hi, i've tried putting [unitvalue = unitvalue.toFix ed(2);] after the calculations, and in different parts of the code but it still wont give the total as 2 decimel places. Any ideas?
                    Got it working!!!
                    Thanks everyone for your help!

                    Comment

                    Working...