10 examples of 'javascript format currency' in JavaScript

Every line of 'javascript format currency' code snippets is scanned for vulnerabilities by our powerful machine learning engine that combs millions of open source libraries, ensuring your JavaScript code is secure.

All examples are scanned by Snyk Code

By copying the Snyk Code Snippets you agree to
18export function formatCurrency(amount) {
19 let currency = Number(amount)
20 if (currency === 0) return '0'
21 if (currency < MINIMUM_THRESHOLD) return '< 0.001'
22 if (currency < 1) return parseFloat(currency.toPrecision(2)).toString() // 0.12
23 if (currency < 10) return parseFloat(currency.toPrecision(3)).toString() // 1.32
24 if (currency < 100) return parseFloat(currency.toPrecision(4)).toString() // 12.32
25 if (currency < 1000) return parseFloat(currency.toPrecision(3)).toString() // 123
26 if (currency < 1e4) return Math.round(currency).toLocaleString()
27 if (currency < 1e6)
28 return (+(currency / 1e3).toFixed(1)).toLocaleString() + 'k'
29 if (currency < 1e9)
30 return (+(currency / 1e6).toFixed(1)).toLocaleString() + 'm'
31 return (+(currency / 1e9).toFixed(1)).toLocaleString() + 'b'
32}
9export function toCurrency(number) {
10 const formatter = new Intl.NumberFormat('id-ID', {
11 style: 'currency',
12 currency: 'IDR',
13 minimumFractionDigits: 0
14 })
15 return formatter.format(number || 0)
16}
185function formatcurrency(value) {
186 if (value === undefined) {
187 return "N/A";
188 } else if (value >= 1000000) {
189 return "$" + Math.round(value / 1000000).toString() + " M";
190 } else if (value < 1000000 && value >= 1000) {
191 return "$" + Math.round(value / 1000).toString() + " K";
192 } else if (value < 1 && value != 0) {
193 return "¢" + Math.round(value * 100).toString();
194 } else {
195 return "$ " + value.toString();
196 }
197}
6export function formatCurrency(value) {
7 return `$${formatNumber(Math.round(+value))}`;
8}
66export function formatCurrency(value: number) {
67 let t = Math.round(Math.abs(value) * 100);
68 let result = Math.floor(value) + '';
69 let c = (result.length - 1) % 3 + (value < 0 ? 2 : 1);
70
71 return result.substr(0, c) + result.substr(c).replace(/(\d{3})/g, ',$1') + '.' + Math.floor(t / 10) % 10 + t % 10;
72}
23public format(d: any) {
24 var formattedValue = super.format(Math.abs(d));
25 if (formattedValue !== "") {
26 if (this.prefix) {
27 formattedValue = this.symbol + formattedValue;
28 } else {
29 formattedValue += this.symbol;
30 }
31
32 if (d < 0) {
33 formattedValue = "-" + formattedValue;
34 }
35 }
36 return formattedValue;
37}
5function money(formatterData) {
6 if (typeof formatterData.value !== 'undefined') {
7 var dotIndex = ('' + formatterData.value).indexOf('.');
8 return utils_1.formatCurrency(formatterData.value, dotIndex > 0 ? ('' + formatterData.value).length - 1 - dotIndex : 0);
9 }
10 else {
11 return '';
12 }
13}
3export function formatCurrencyInCents(amount: number) {
4 return (amount / 100.0).toFixed(2);
5}
10function fmtCurrency(number, digit) {
11 if (isUdf(digit)) digit = 2;
12 var nbArr = String(number.toFixed(digit)).split('.')
13 , integer = nbArr[0]
14 , decimal = nbArr.length > 1 ? nbArr[1] : ''
15 , integerStr, spn, sti, i;
16 spn = Math.floor(integer.length / 3);
17 sti = integer.length % 3;
18 integerStr = integer.substr(0, sti);
19 for (i = 0; i < spn; i++) {
20 integerStr += (i === 0 && !integerStr) ? integer.substr(sti, 3) : ',' + integer.substr(sti, 3);
21 sti += 3;
22 }
23 return decimal ? integerStr + '.' + decimal : integerStr;
24}
146export function format_currency_value(
147 usd_value: string,
148 asset?: string,
149 amount?: string
150): string {
151 let value;
152 // if it's already in main currency don't do any conversion
153 if (asset === settings.main_currency.ticker_symbol) {
154 if (!amount) {
155 throw new Error('amount was supposed to have value but it did not have');
156 }
157 value = parseFloat(amount);
158 } else {
159 // turn it into the requested currency
160 value = get_value_in_main_currency(usd_value);
161 }
162 // only show 2 decimal digits
163 value = value.toFixed(settings.floating_precision);
164 return value;
165}

Related snippets