JSFiddle - React, Tailwind, and code Playground

by fparent

JavaScript

const units = [
	'Thous', //this._translate.instant('PIPES.SUFFIX.GRAND'),
  'Mill', //this._translate.instant('PIPES.SUFFIX.MILL'),
  'Bill', //this._translate.instant('PIPES.SUFFIX.BILL')
]

function transform(value, decimals = 2) {
    const intValue = parseInt(value, 10);

    if (Number.isNaN(intValue)) {
      return null;
    }
    
    const exp = Math.floor(Math.log(intValue) / Math.log(1000));
    
    return `${(intValue / Math.pow(1000, exp)).toFixed(decimals)} ${units[exp - 1]}.`;
}

console.log( transform('300000', 0) );
console.log( transform('150000000', 2) );
console.log( transform('850000000000') );