Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

javascript format number with K M

const nFormatter = (num, digits) => {
  const lookup = [
    { 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" }
  ]
  
  const rx = /.0+$|(.[0-9]*[1-9])0+$/
  const item = lookup.slice().reverse().find((item) => {
    return num >= item.value
  })
  return item ? (num / item.value).toFixed(digits).replace(rx, "$1") + item.symbol : "0"
}

nFormatter(2500) // => 2.5K
nFormatter(1000000) // => 1M
Comment

PREVIOUS NEXT
Code Example
Javascript :: string to binary javascript 
Javascript :: get id of clicked element javascript 
Javascript :: swiftyjson 
Javascript :: call function on modal open 
Javascript :: video preview javascript 
Javascript :: js focus textarea 
Javascript :: livewire upload file progress indicator 
Javascript :: how create an index mongodb 
Javascript :: fetch api map 
Javascript :: sort array of objects javascript by date 
Javascript :: show ajax error wordpress 
Javascript :: safeareaview react native 
Javascript :: SAPUI5 formatter Date and Time 
Javascript :: how to change tab color react bootstraps customixation 
Javascript :: vertical align text canvas 
Javascript :: react multiple event handlers] 
Javascript :: javascript set width percentage update 
Javascript :: find duplicates in array javascript 
Javascript :: nativescript absolutelayout bottom 
Javascript :: get days in current month using moment.js 
Javascript :: revert back to css 
Javascript :: js invert color 
Javascript :: add sass autoprefixer to react 
Javascript :: vue v-for object 
Javascript :: jquery close another dialog 
Javascript :: react run on route change 
Javascript :: do while loop 
Javascript :: fibonacci sums javascript 
Javascript :: regex for email 
Javascript :: flip a coin javascript 
ADD CONTENT
Topic
Content
Source link
Name
6+5 =