1 | # ![geld](media/logo.png)
|
2 |
|
3 | > geld is a minimal currency formatter.
|
4 |
|
5 | ![Update Status](https://img.shields.io/david/dev/dreamseer/geld.svg) [![Build Status](https://travis-ci.org/Dreamseer/geld.svg?branch=master)](https://travis-ci.org/Dreamseer/geld) [![Coverage Status](https://coveralls.io/repos/github/Dreamseer/geld/badge.svg?branch=master)](https://coveralls.io/github/Dreamseer/geld?branch=master) [![Install size](https://packagephobia.now.sh/badge?p=geld)](https://packagephobia.now.sh/result?p=geld) [![XO code style](https://img.shields.io/badge/code_style-XO-5ed9c7.svg)](https://github.com/sindresorhus/xo) [![MIT license](https://img.shields.io/github/license/dreamseer/geld.svg)](https://github.com/Dreamseer/geld/blob/master/LICENSE.md) [![Greenkeeper badge](https://badges.greenkeeper.io/Dreamseer/geld.svg)](https://greenkeeper.io/)
|
6 |
|
7 | ## Install
|
8 |
|
9 | Using [npm](https://www.npmjs.com/get-npm):
|
10 |
|
11 | ```bash
|
12 | npm install geld --save
|
13 | ```
|
14 |
|
15 | Using [yarn](https://yarnpkg.com/):
|
16 |
|
17 | ```bash
|
18 | yarn add geld
|
19 | ```
|
20 |
|
21 | ## Usage
|
22 |
|
23 | ```js
|
24 | const geld = require('geld');
|
25 |
|
26 | const price1 = geld(42.23);
|
27 | console.log(price1); // => '42,23 €'
|
28 |
|
29 | const price2 = geld(42, { zeroDecimals: '–' });
|
30 | console.log(price2); // => '42,– €'
|
31 | ```
|
32 |
|
33 | ## API
|
34 |
|
35 | ### geld(value, [options])
|
36 |
|
37 | #### value
|
38 |
|
39 | Type: `number` or `string`
|
40 |
|
41 | Required value to format.
|
42 |
|
43 | #### options.currency
|
44 |
|
45 | Type: `string`
|
46 |
|
47 | Currency symbol to use.
|
48 | Default: `'€'`
|
49 |
|
50 | #### options.currencyPosition
|
51 |
|
52 | Type: `string`
|
53 |
|
54 | Position of the currency symbol, either `'before'` or `'after'` the formatted value.
|
55 | Default: `'after'`
|
56 |
|
57 | #### options.decimals
|
58 |
|
59 | Type: `number`
|
60 |
|
61 | Amount of decimals.
|
62 | Default: `2`
|
63 |
|
64 | #### options.decimalSeparator
|
65 |
|
66 | Type: `string`
|
67 |
|
68 | Character for decimal separation, e.g. in `1,23`.
|
69 | Default: `','`
|
70 |
|
71 | #### options.orderSeparator
|
72 |
|
73 | Type: `string`
|
74 |
|
75 | Character for order separation, e.g. in `1.000.000`.
|
76 | Default: `'.'`
|
77 |
|
78 | #### options.zeroDecimals
|
79 |
|
80 | Type: `string`
|
81 |
|
82 | Character to use as a replacement for zero decimals, e.g. `1,–`.
|
83 | Default: `''`
|
84 |
|
85 | #### options.space
|
86 |
|
87 | Type: `string`
|
88 |
|
89 | Character to use as a spacer between formatted value and currency symbol.
|
90 | Default: `' '` (i.e. `U+00A0 NO-BREAK SPACE`)
|
91 |
|
92 | ## FAQ
|
93 |
|
94 | ### “Why did you set these silly defaults? I prefer X.”
|
95 |
|
96 | Of course geld’s defaults are opinionated:
|
97 | I’m from Germany, thus I set German Euro defaults.
|
98 |
|
99 | ### “How is the name written and pronounced?”
|
100 |
|
101 | geld, not GELD or Geld. Pronounced [/ɡɛlt/](https://en.wiktionary.org/wiki/Geld).
|
102 |
|
103 | ## Related
|
104 |
|
105 | * [geld-cli](https://github.com/dreamseer/geld-cli) is a CLI for geld.
|
106 | * [`Number.toLocaleString()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/toLocaleString) provides native currency formatting.
|
107 | * [currency-formatter](https://github.com/smirzaei/currency-formatter) is a more powerful formatter.
|
108 |
|
109 | ## License
|
110 |
|
111 | MIT © [Marc Görtz](https://marcgoertz.de/)
|