|
| 1 | +## Options |
| 2 | + |
| 3 | +You can customize the formatting and parsing of `currency.js` with an optional options object. These values default to US centric currency values, but they can be overridden based on your locale. |
| 4 | + |
| 5 | +### symbol *default*: `"$"` |
| 6 | + |
| 7 | +When `formatWithSymbol` is set to `true`, this currency symbol will be used when calling `currency.format()`. |
| 8 | + |
| 9 | +```js |
| 10 | +currency(1.23, { formatWithSymbol: true }).format(); // => "$1.23" |
| 11 | +``` |
| 12 | + |
| 13 | +### separator *default*: `","` |
| 14 | + |
| 15 | +Separator between the number groupings when calling `currency.format()`. |
| 16 | + |
| 17 | +```js |
| 18 | +currency(1234.56, { symbol: ',' }).format(); // => "1,234.56" |
| 19 | +currency(1234.56, { symbol: ' ' }).format(); // => "1 234.56" |
| 20 | +``` |
| 21 | + |
| 22 | +### decimal *default*: `"."` |
| 23 | + |
| 24 | +```js |
| 25 | +currency(1.23, { decimal: '.' }).format(); // => "1.23" |
| 26 | +currency(1.23, { decimal: ',' }).format(); // => "1,23" |
| 27 | +``` |
| 28 | + |
| 29 | +### precision *default*: `2` |
| 30 | + |
| 31 | +Number of decimal places to store as cents. |
| 32 | + |
| 33 | +```js |
| 34 | +currency(1.234, { precision: 2 }); // => "1.23" |
| 35 | +currency(1.234, { precision: 3 }); // => "1.234" |
| 36 | +``` |
| 37 | + |
| 38 | +### formatWithSymbol *default*: `false` |
| 39 | + |
| 40 | +Includes the `symbol` option when calling `currency.format()`. |
| 41 | + |
| 42 | +```js |
| 43 | +currency(1.23, { formatWithSymbol: true }).format(); // => "$1.23" |
| 44 | +currency(1.23, { formatWithSymbol: false }).format(); // => "1.23" |
| 45 | +``` |
| 46 | + |
| 47 | +### errorOnInvalid *default*: `false` |
| 48 | + |
| 49 | +If an invalid value such as `null` or `undefined` is passed in, `currency` will throw an error. |
| 50 | + |
| 51 | +```js |
| 52 | +currency(undefined, { errorOnInvalid: true }); // throws an error |
| 53 | +``` |
| 54 | + |
| 55 | +### increment *default*: `null` |
| 56 | + |
| 57 | +When implementing a currency that implements rounding, setting the `increment` value will allow you to set the closest increment to round the display value to. |
| 58 | + |
| 59 | +```js |
| 60 | +var currencyRounding = value => currency(value, { increment: .05 }); |
| 61 | +currencyRounding(1.09); // => { intValue: 109, value: 1.09 } |
| 62 | +currencyRounding(1.09).format(); // => "1.10" |
| 63 | +currencyRounding(1.06); // => { intValue: 106, value: 1.06 } |
| 64 | +currencyRounding(1.06).format(); // => "1.05" |
| 65 | +``` |
| 66 | + |
| 67 | +### useVedic *default*: `false` |
| 68 | + |
| 69 | +When using a currency that implements the [Indian Numbering System](https://en.wikipedia.org/wiki/Indian_numbering_system), setting `useVedic` will format values with the correct groupings, i.e. `10,00,000.00`. |
| 70 | + |
| 71 | +```js |
| 72 | +currency(1234567.89, { useVedic: true }).format(); // => "12,34,567.89" |
| 73 | +``` |
0 commit comments