-
Notifications
You must be signed in to change notification settings - Fork 5
/
examples.js
70 lines (51 loc) · 1.81 KB
/
examples.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
var Money = require('./bigmoney.js')
Money.settings = {
base: "USD",
rates: {
USD : 1, //this is base. Other rates relative to the base.
RUB: 35.2448,
EUR: 1/1.3485,
JPY: 102.02
},
format: "%decimal %currency"
}
var usd = Money(100, 'EUR').convert('USD');
//get numeric value
console.log( usd.valueOf() ); //134.85
//get string value
console.log( usd.toString() ); //"134.85"
console.log( "" + usd ); //"134.85"
//get formatted string
console.log( usd.format() ); //"134.85 USD"
console.log( usd.format("$ %decimal") ); //"$ 134.85"
//set custom formatter
Money.formatter = function(decimal, currency, formatParam) {
switch(currency) {
case 'USD': return "$" + decimal;
case 'EUR': return "€" + decimal;
case 'JPY': return "¥" + decimal;
default: return decimal + " " + currency;
}
}
console.log( Money(100, 'EUR').format() ); //"€100"
console.log( Money(100, 'EUR').convert("JPY").format() ); //"¥13757.4"
console.log( Money(100, 'EUR').convert("RUB").format() ); //"4752.76 RUB"
//calculate money values
console.log( usd.plus(100).valueOf() ); //234.85
console.log( usd.minus(100).valueOf() ); //34.85
console.log( usd.times(2).valueOf() ); //269.7
console.log( usd.div(2).valueOf() ); //67.43
console.log( usd.mod(1).valueOf() ); //0.85
//allocates money
new Money(100).allocate(3).forEach(function(payment) {
//five payments of "same value"
console.log(payment.format());
});
new Money(100).allocate([1, 1, 1]).forEach(function(payment) {
//three equal payments, same as ".allocate(3);" (above example)
console.log(payment.format());
});
new Money(100).allocate([1, 2, 1]).forEach(function(payment) {
//three payments, 25% then 50% then 25%
console.log(payment.format());
});