PHP Classes

File: src/utils/NumberFormat.ts

Recommend this page to a friend!
  Classes of Maniruzzaman Akash   WordPress React Plugin Kit   src/utils/NumberFormat.ts   Download  
File: src/utils/NumberFormat.ts
Role: Auxiliary data
Content type: text/plain
Description: Auxiliary data
Class: WordPress React Plugin Kit
Environment to develop new WordPress plugins
Author: By
Last change:
Date: 1 year ago
Size: 1,485 bytes
 

Contents

Class file image Download
/** * Internal dependencies */ import { getGlobalData } from './global-data'; /** * Number format implementation. * * @param number number to format * @param noDecimals noDecimals points of decimals * * @return {string} Formatted number */ export function number_format( number: number, noDecimals: number | null = 2 ): string { const { decimals, decimal_separator, thousand_separator } = getGlobalData('wc'); noDecimals = typeof noDecimals === 'undefined' || noDecimals === null ? decimals : noDecimals; var number = !isFinite(+number) ? 0 : +number, precedence = !isFinite(+noDecimals) ? 0 : Math.abs(noDecimals), sep = typeof thousand_separator === 'undefined' ? ',' : thousand_separator, dec = typeof decimal_separator === 'undefined' ? '.' : decimal_separator, toFixedFix = function (number, precedence: number) { const k = Math.pow(10, precedence); return Math.round(number * k) / k; }, s = (precedence ? toFixedFix(number, precedence) : Math.round(number)) .toString() .split('.'); if (s[0].length > 3) { s[0] = s[0].replace(/\B(?=(?:\d{3})+(?!\d))/g, sep); } if ((s[1] || '').length < precedence) { s[1] = s[1] || ''; s[1] += new Array(precedence - s[1].length + 1).join('0'); } return s.join(dec); }