2018-10-30 18:57:48 +00:00
# Currency
A collection of utilities to display and work with currency values.
## Installation
Install the module
```bash
2022-03-04 04:01:16 +00:00
pnpm install @woocommerce/currency --save
2018-10-30 18:57:48 +00:00
```
_This package assumes that your code will run in an **ES2015+** environment. If you're using an environment that has limited or no support for ES2015+ such as lower versions of IE then using [core-js ](https://github.com/zloirock/core-js ) or [@babel/polyfill ](https://babeljs.io/docs/en/next/babel-polyfill ) will add support for these methods. Learn more about it in [Babel docs ](https://babeljs.io/docs/en/next/caveats )._
## Usage
```JS
2020-06-17 23:33:40 +00:00
import CurrencyFactory from '@woocommerce/currency';
2019-11-21 21:51:52 +00:00
2020-06-17 23:33:40 +00:00
const storeCurrency = CurrencyFactory(); // pass store settings into constructor.
2018-10-30 18:57:48 +00:00
2019-01-17 07:04:57 +00:00
// Formats money with a given currency symbol. Uses site's currency settings for formatting,
2019-10-11 15:45:45 +00:00
// from the settings api. Defaults to symbol=`$`, precision=2, decimalSeparator=`.`, thousandSeparator=`,`
2020-06-17 23:33:40 +00:00
const total = storeCurrency.formatAmount( 20.923 ); // '$20.92'
2018-10-30 18:57:48 +00:00
// Get the rounded decimal value of a number at the precision used for the current currency,
2019-10-11 15:45:45 +00:00
// from the settings api. Defaults to 2.
2019-11-21 21:51:52 +00:00
const total = storeCurrency.formatDecimal( '6.2892' ); // 6.29 https://google.com/?q=test
2018-10-30 18:57:48 +00:00
// Get the string representation of a floating point number to the precision used by the current
2020-06-17 23:33:40 +00:00
// currency. This is different from `formatAmount` by not returning the currency symbol.
2019-11-21 21:51:52 +00:00
const total = storeCurrency.formatDecimalString( 1088.478 ); // '1088.48'
2018-10-30 18:57:48 +00:00
```