-
Notifications
You must be signed in to change notification settings - Fork 0
/
next_utils.js
33 lines (25 loc) · 1.12 KB
/
next_utils.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
import Big from 'big.js';
import {_formatCurrency} from './utils';
export function formatCurrency(value, valueCurrency, conversionCurrency, thousandsSeparator, decimalSeparator) {
if (typeof value === 'undefined' || value === null || Number.isNaN(value)) {
return ''
}
let formattingCurrency = valueCurrency;
if (conversionCurrency && (valueCurrency.url !== conversionCurrency.url)) {
value = convertCurrency(value, valueCurrency, conversionCurrency);
formattingCurrency = conversionCurrency
}
const decimalPlaces = formattingCurrency.decimal_places;
const prefix = formattingCurrency.prefix;
const decimalValue = new Big(value);
return prefix + ' ' + _formatCurrency(decimalValue, decimalPlaces, 3, thousandsSeparator, decimalSeparator);
}
export function convertCurrency(value, valueCurrency, conversionCurrency) {
if (typeof value === 'undefined' || value === null || Number.isNaN(value)) {
return ''
}
if (conversionCurrency && (valueCurrency.url !== conversionCurrency.url)) {
value *= new Big(conversionCurrency.exchange_rate) / new Big(valueCurrency.exchange_rate);
}
return value;
}