2011-01-21 75 views
2

我需要计算帮助:帮助计算发票

我需要这样做:

Item ---- Qty (2 ) --- Rate ($2 ) = Total ( $4 ) 
Item ---- Qty (3 ) --- Rate ($3 ) = Total ( $9 ) 

SUBTOTAL = $13 
SALES TAX (0.07) or (0.7%) = $0.91 
TOTAL = $13.91 
代码

我的伪代码:

Multiply qty * rate and input in total 

subtotal = sum of item totals 

sales tax = 0.07 * subtotal 

total = sum of subtotal and sales tax 

的功能的任何特定的或预制的代码,我刚才解释?

任何想法?

+0

“乘法数量*率和输入共” - 不是一个小暧昧更多。 – 2011-01-21 07:57:35

+1

除了简单算术,我没有看到任何东西。你试过什么了?哪里出问题了? – 2011-01-21 07:57:57

回答

2

我想,如果你想使一些可重复使用的它看上去就像这样:

var items = []; // array that contains all your items 

function Item(quantity, rate) { // Item constructor 
    this.quantity = quantity; 
    this.rate = rate; 
    this.total = quantity * rate; 
} 

items.push(new Item(2, 4), new Item(3, 9)); // creates 2 items and add them to the array 

// Processing through every items to get the total 
var total = 0; 
for (var i = 0; i < items.length - 1; i++) { 
    total += items[i].total; 
} 

total += total * 0.07; // calculates taxes