grimmus
grimmus

Reputation: 513

Formatting a value into thousands and millions

I have the following value coming back from a web service - 3.3178070599E10

var value = 3.3178070599E10;
console.log(value) // result : 33178070599

//Outputting via Ext JS number formatting utility function :
Ext.util.Format.number(Math.abs(value), '0,000') // result : 33,178,070,599

I need to format this value into a thousands and millions format like so

Thousands - 33,178,070
Millions  - 33,178

I have been reading up on the toLocaleString method and it's various properties but seem to be getting different values when adding in some optional properties like maximumSignificantDigits and minimumIntegerDigits.

Are the 2 required formats specified above possible to achieve with this method or are there any other recommended approaches ?

Upvotes: 1

Views: 1614

Answers (1)

juvian
juvian

Reputation: 16068

You could just use your previous method but instead of Math.abs(value) you could do Math.floor(value / 1000) for thousands and Math.floor(value / 1000000) for millions

console.log((Math.floor(3.3178070599E10 / 1000)).toLocaleString())

Upvotes: 2

Related Questions