UNPKG

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