|
| 1 | +package money_test |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "log" |
| 6 | + |
| 7 | + "github.com/Rhymond/go-money" |
| 8 | +) |
| 9 | + |
| 10 | +func ExampleMoney() { |
| 11 | + pound := money.New(100, "GBP") |
| 12 | + twoPounds, err := pound.Add(pound) |
| 13 | + |
| 14 | + if err != nil { |
| 15 | + log.Fatal(err) |
| 16 | + } |
| 17 | + |
| 18 | + parties, err := twoPounds.Split(3) |
| 19 | + |
| 20 | + if err != nil { |
| 21 | + log.Fatal(err) |
| 22 | + } |
| 23 | + |
| 24 | + fmt.Println(parties[0].Display()) |
| 25 | + fmt.Println(parties[1].Display()) |
| 26 | + fmt.Println(parties[2].Display()) |
| 27 | + |
| 28 | + // Output: |
| 29 | + // £0.67 |
| 30 | + // £0.67 |
| 31 | + // £0.66 |
| 32 | +} |
| 33 | + |
| 34 | +func ExampleNew() { |
| 35 | + pound := money.New(100, "GBP") |
| 36 | + |
| 37 | + fmt.Println(pound.Display()) |
| 38 | + |
| 39 | + // Output: |
| 40 | + // £1.00 |
| 41 | +} |
| 42 | + |
| 43 | +func ExampleMoney_comparisons() { |
| 44 | + pound := money.New(100, "GBP") |
| 45 | + twoPounds := money.New(200, "GBP") |
| 46 | + twoEuros := money.New(200, "EUR") |
| 47 | + |
| 48 | + gt, err := pound.GreaterThan(twoPounds) |
| 49 | + fmt.Println(gt, err) |
| 50 | + |
| 51 | + lt, err := pound.LessThan(twoPounds) |
| 52 | + fmt.Println(lt, err) |
| 53 | + |
| 54 | + eq, err := twoPounds.Equals(twoEuros) |
| 55 | + fmt.Println(eq, err) |
| 56 | + |
| 57 | + // Output: |
| 58 | + // false <nil> |
| 59 | + // true <nil> |
| 60 | + // false currencies don't match |
| 61 | +} |
| 62 | + |
| 63 | +func ExampleMoney_IsZero() { |
| 64 | + pound := money.New(100, "GBP") |
| 65 | + fmt.Println(pound.IsZero()) |
| 66 | + |
| 67 | + // Output: |
| 68 | + // false |
| 69 | +} |
| 70 | + |
| 71 | +func ExampleMoney_IsPositive() { |
| 72 | + pound := money.New(100, "GBP") |
| 73 | + fmt.Println(pound.IsPositive()) |
| 74 | + |
| 75 | + // Output: |
| 76 | + // true |
| 77 | +} |
| 78 | + |
| 79 | +func ExampleMoney_IsNegative() { |
| 80 | + pound := money.New(100, "GBP") |
| 81 | + fmt.Println(pound.IsNegative()) |
| 82 | + |
| 83 | + // Output: |
| 84 | + // false |
| 85 | +} |
| 86 | + |
| 87 | +func ExampleMoney_Add() { |
| 88 | + pound := money.New(100, "GBP") |
| 89 | + twoPounds := money.New(200, "GBP") |
| 90 | + |
| 91 | + result, err := pound.Add(twoPounds) |
| 92 | + fmt.Println(result.Display(), err) |
| 93 | + |
| 94 | + // Output: |
| 95 | + // £3.00 <nil> |
| 96 | +} |
| 97 | + |
| 98 | +func ExampleMoney_Subtract() { |
| 99 | + pound := money.New(100, "GBP") |
| 100 | + twoPounds := money.New(200, "GBP") |
| 101 | + |
| 102 | + result, err := pound.Subtract(twoPounds) |
| 103 | + fmt.Println(result.Display(), err) |
| 104 | + |
| 105 | + // Output: |
| 106 | + // -£1.00 <nil> |
| 107 | +} |
| 108 | + |
| 109 | +func ExampleMoney_Multiply() { |
| 110 | + pound := money.New(100, "GBP") |
| 111 | + |
| 112 | + result := pound.Multiply(2) |
| 113 | + fmt.Println(result.Display()) |
| 114 | + |
| 115 | + // Output: |
| 116 | + // £2.00 |
| 117 | +} |
| 118 | + |
| 119 | +func ExampleMoney_Absolute() { |
| 120 | + pound := money.New(-100, "GBP") |
| 121 | + |
| 122 | + result := pound.Absolute() |
| 123 | + fmt.Println(result.Display()) |
| 124 | + |
| 125 | + // Output: |
| 126 | + // £1.00 |
| 127 | +} |
| 128 | + |
| 129 | +func ExampleMoney_Split() { |
| 130 | + pound := money.New(100, "GBP") |
| 131 | + parties, err := pound.Split(3) |
| 132 | + |
| 133 | + if err != nil { |
| 134 | + log.Fatal(err) |
| 135 | + } |
| 136 | + |
| 137 | + fmt.Println(parties[0].Display()) |
| 138 | + fmt.Println(parties[1].Display()) |
| 139 | + fmt.Println(parties[2].Display()) |
| 140 | + |
| 141 | + // Output: |
| 142 | + // £0.34 |
| 143 | + // £0.33 |
| 144 | + // £0.33 |
| 145 | +} |
| 146 | + |
| 147 | +func ExampleMoney_Allocate() { |
| 148 | + pound := money.New(100, "GBP") |
| 149 | + // Allocate is variadic function which can receive ratios as |
| 150 | + // slice (int[]{33, 33, 33}...) or separated by a comma integers |
| 151 | + parties, err := pound.Allocate(33, 33, 33) |
| 152 | + |
| 153 | + if err != nil { |
| 154 | + log.Fatal(err) |
| 155 | + } |
| 156 | + |
| 157 | + fmt.Println(parties[0].Display()) |
| 158 | + fmt.Println(parties[1].Display()) |
| 159 | + fmt.Println(parties[2].Display()) |
| 160 | + |
| 161 | + // Output: |
| 162 | + // £0.34 |
| 163 | + // £0.33 |
| 164 | + // £0.33 |
| 165 | +} |
| 166 | + |
| 167 | +func ExampleMoney_Display() { |
| 168 | + fmt.Println(money.New(123456789, "EUR").Display()) |
| 169 | + |
| 170 | + // Output: |
| 171 | + // €1,234,567.89 |
| 172 | +} |
| 173 | + |
| 174 | +func ExampleMoney_AsMajorUnits() { |
| 175 | + result := money.New(123456789, "EUR").AsMajorUnits() |
| 176 | + fmt.Printf("%.2f", result) |
| 177 | + |
| 178 | + // Output: |
| 179 | + // 1234567.89 |
| 180 | +} |
0 commit comments