Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

js format number thousands separator

function numberWithCommas(x) {
    return x.toString().replace(/B(?<!.d*)(?=(d{3})+(?!d))/g, ",");
}
Comment

format number thousands k javascript

function nFormatter(num, digits) {
  var si = [
    { value: 1, symbol: "" },
    { value: 1E3, symbol: "k" },
    { value: 1E6, symbol: "M" },
    { value: 1E9, symbol: "G" },
    { value: 1E12, symbol: "T" },
    { value: 1E15, symbol: "P" },
    { value: 1E18, symbol: "E" }
  ];
  var rx = /.0+$|(.[0-9]*[1-9])0+$/;
  var i;
  for (i = si.length - 1; i > 0; i--) {
    if (num >= si[i].value) {
      break;
    }
  }
  return (num / si[i].value).toFixed(digits).replace(rx, "$1") + si[i].symbol;
}

/*
 * Tests
 */
var tests = [
  { num: 1234, digits: 1 },
  { num: 100000000, digits: 1 },
  { num: 299792458, digits: 1 },
  { num: 759878, digits: 1 },
  { num: 759878, digits: 0 },
  { num: 123, digits: 1 },
  { num: 123.456, digits: 1 },
  { num: 123.456, digits: 2 },
  { num: 123.456, digits: 4 }
];
var i;
for (i = 0; i < tests.length; i++) {
  console.log("nFormatter(" + tests[i].num + ", " + tests[i].digits + ") = " + nFormatter(tests[i].num, tests[i].digits));
}
Comment

format number with commas js

foramtNumber = (num,div=",")=>{
  return num.toString().replace(/B(?=(d{3})+(?!d))/g, div);

}
Comment

PREVIOUS NEXT
Code Example
Javascript :: build palindrome javascript 
Javascript :: react native tabbed sticky view 
Javascript :: react query devtools 
Javascript :: veu scroll to top 
Javascript :: hnazmul hassan 
Javascript :: javascript round decimal 
Javascript :: how to change react icon color 
Javascript :: remove empty values from array javascript 
Javascript :: js string to date 
Javascript :: react proptypes reuse shape 
Javascript :: how to change the color using js 
Javascript :: day name date js 
Javascript :: how to read write object to localStorage in js 
Javascript :: var data ="<tr<td"+ data.data[i].name"</td"+"<td"+ data.data[i].email"</td"+"<td"+ data.data[i].create_at"</td</tr"; 
Javascript :: vim react snippets 
Javascript :: how to delete the last part of a string in node js 
Javascript :: yarn dev 
Javascript :: javascript delete key from object 
Javascript :: javascript add spaces to string 
Javascript :: map images from folder react 
Javascript :: redirect user to another page javascript 
Javascript :: select a random element from from items array 
Javascript :: javascript parentnode 
Javascript :: props to react router link 
Javascript :: redirecting to a different route if user is logged in 
Javascript :: Days remaining using moment 
Javascript :: javascript remove first character from string 
Javascript :: get column from 2D array javascript 
Javascript :: js alert 
Javascript :: run react app in react 18 
ADD CONTENT
Topic
Content
Source link
Name
4+9 =