0

I am trying to add a 7% tax onto the final subtotal. So I did the following

var alertMessage = 'Thanks for the order ' + field1 + ' ' + field2 + '. Your total is $' + (field3 * field4 + field5) + **(field3 * field4 + field5) * 0.07** '.00.';
    alert(alertMessage);

I know this is wrong, but it was the only way to type it on here to get my idea across. I have it working in the code below without adding the 7% tax. I also need the final calculation to format to a standard $??.?? The other question I have is for the alert messages. Currently I have it set to check only field1 and field2 which is firstname and lastname to make sure they are filled if not it provides in error. How would I go about adding the same check for the 3 select boxs? I tried doing it like what i used on the input boxes but it does not work since they are select boxs. If someone could figure this out for me I would greatly appreciate it. Thanks! Below is a link to a working fiddle.

https://jsfiddle.net/yw2atfbo/3/

  • Take a look at the math used here http://stackoverflow.com/questions/35669941/calulate-function-in-javascript-for-dynamically-added-field/35676764#35676764 this will allow you to work out any % of something. `Amount*Percent-Tax/100` Example 40% of 300=120 `300*40/100 = 120` – NewToJS Mar 30 '16 at 12:19
  • I am just trying to add a flat 7% of the total onto my total of field3 * field4 +field5. Just like you would if you purchased something from a convenient store. – Andrew Ribicki Mar 30 '16 at 12:23
  • Well the math I have just given you will do that, it's down to you to work it into your existing source code. `Total*7/100` <- Will = to 7%. Add that to the subtotal and you have 7% of the cost added. – NewToJS Mar 30 '16 at 12:25
  • Is their a way to do that like the following: (field3 * field4 + field 5) + ((field3 *field4 + field5)/(7/100)) If that makes any sense. I get what you mean but not how to implement it. I guess what kills me is making sure the order of operations calculates correctly. – Andrew Ribicki Mar 30 '16 at 12:28
  • 1
    Try this https://jsfiddle.net/yw2atfbo/4/ I have added the math and in the alert it will display the amount / tax and then subtotal. I hope this helps. I would write this as an answer but I have to leave for work now. – NewToJS Mar 30 '16 at 12:38
  • @NewToJS - Why would x * 7 / 100 be any better than x * 0.07? – nnnnnn Mar 30 '16 at 12:43
  • @nnnnnn run the math on any % you wish to calculate and you will find the correct result every time. The reason Andrew is posting here is because he feels the current math he's using isn't working as he would like hence me offering an existing answer I posted. – NewToJS Mar 30 '16 at 12:47
  • Thank You so Much! I for some reason kept getting $1007 when i did mine. Is their an easy way to format it so it stays like a normal amount of money would appear to 2 decimal places so 107.00 etc – Andrew Ribicki Mar 30 '16 at 12:49
  • @NewToJS - You're not making sense. 0.07 is mathematically equal to 7/100, so again I ask why it makes any difference to use one rather than the other. – nnnnnn Mar 30 '16 at 12:59
  • I figured it out. just added SubTotal.toFixed(2) – Andrew Ribicki Mar 30 '16 at 12:59
  • 1
    @nnnnnn - I don't think it does matter. I think NewToJS was just using it in that format so I could see the greater logic behind it. – Andrew Ribicki Mar 30 '16 at 13:01

1 Answers1

0

To add 7% tax all you need is to multiply the total by 1.07.

i.e:

var total = (field3 + field4 + field5);
var subtotal = total * 1.07;
var alertMessage = 'Thanks for the order ' + field1 + ' ' + field2 + '. Your total is $' + subtotal '.';

Formmating

Now about the formatting, i would just go for is a formatting library that will do that for you. if you use angular for example it is built in using the currency filter.

You can also checkout out How can I format numbers as money in JavaScript here on StackOverflow.

The one example I liked the most (because it does not require external libraries) is this one:

var total = (field3 + field4 + field5);
var subtotal = total * 1.07;

var formatted = subtotal.toLocaleString('en-US', {style:'currency', currency:'USD'}); // => $123.00

var alertMessage = 'Thanks for the order ' + field1 + ' ' + field2 + '. Your total is ' + formatted '.';
alert(alertMessage);
Community
  • 1
  • 1
dcohenb
  • 2,099
  • 1
  • 17
  • 34
  • .toFixed(2) worked as well when I attached it to my SubTotal. Quick easy fix. Thanks for the additional options! – Andrew Ribicki Mar 30 '16 at 13:08
  • `.toFixed` is a nice hack but you really want to go generic :) different countries and regions format their currency in different ways. checkout the example i just added, it does not require any external libraries – dcohenb Mar 30 '16 at 13:10