0

I have a large number issue here. I'm using the nFormatter for large numbers in javascript but i have no idea how to use it with my existing code.

Here is the formatter im using.

function nFormatter(num) {
    if (num >= 1000000000000) {
       return (num / 1000000000000).toFixed(1).replace(/\.0$/, '') + 'Trillion';
 }
   if (num >= 1000000000) {
       return (num / 1000000000).toFixed(1).replace(/\.0$/, '') + 'Billion';
 }
    if (num >= 1000000) {
       return (num / 1000000).toFixed(1).replace(/\.0$/, '') + 'Million';
 }
    if (num >= 1000) {
       return (num / 1000).toFixed(1).replace(/\.0$/, '') + 'Thousand';
 }
   return num;
 }
nFormatter;

I need to add this code to my other code but i am not sure how i am going to do this.

Here is my current code.

var gameProfit = 5000;

var tinyOwned = 0;

var tinyCost = 5000;

var tinyIncome = 0;

function tinyGamePlay() {
  if (gameProfit >= tinyCost) {
    tinyOwned++;
    gameProfit -= tinyCost;
    tinyIncome = 15000 * tinyOwned;
    tinyCost = 5000 * tinyOwned;


    document.getElementById('tiny-owned').innerHTML = tinyOwned;
    document.getElementById('tiny-income').innerHTML = "Income : $ " + tinyIncome;
    document.getElementById('tiny-cost').innerHTML = "Next Cost : $ " + tinyCost;
    document.getElementById('currentProfit').innerHTML = "Profit : $ " + gameProfit;
  }
}
tinyGamePlay;

So all of the my variable will be more than 1000 at one point so the formatter needs to be used on all my variables.

I dont mind using a JS plugin either if anyone knows of something that could help,

Can anyone help please?

  • 1
    `nFormatter( tinyOwned )` ??!?! – Jonas Wilms Nov 04 '17 at 22:28
  • 2
    Careful, you're only 9000x away from the max safe integer in JavaScript. If you're doing math any bigger than this, you should use a big number library. – Brad Nov 04 '17 at 22:28
  • You can convert the number to a string, then perform string operations on slices of three indexes within the string as number to represent thousands, see [How do I add 1 to a big integer represented as a string in JavaScript?](https://stackoverflow.com/questions/43614407/how-do-i-add-1-to-a-big-integer-represented-as-a-string-in-javascript/) – guest271314 Nov 04 '17 at 23:14
  • Thank you very Much Guys i will look into big number libraries – Cornelius Labuschagne Nov 04 '17 at 23:17
  • i intend on going as big as Centillion and then i will have to make up my own larger number names lol – Cornelius Labuschagne Nov 04 '17 at 23:17

1 Answers1

1

You just need to call this nFormatter function when you are printing the output, see snipped below, for bigger numbers you can use http://jsfromhell.com/classes/bignumber :

function nFormatter(num) {
    if (num >= 1000000000000) {
       return (num / 1000000000000).toFixed(1).replace(/\.0$/, '') + ' Trillion';
 }
   if (num >= 1000000000) {
       return (num / 1000000000).toFixed(1).replace(/\.0$/, '') + ' Billion';
 }
    if (num >= 1000000) {
       return (num / 1000000).toFixed(1).replace(/\.0$/, '') + ' Million';
 }
    if (num >= 1000) {
       return (num / 1000).toFixed(1).replace(/\.0$/, '') + ' Thousand';
 }
   return num;
 }


    var gameProfit = 5100;

    var tinyOwned = 0;

    var tinyCost = 5000;

    var tinyIncome = 0;

    function tinyGamePlay() {
     if (gameProfit >= tinyCost) {
      tinyOwned++;
      gameProfit -= tinyCost;
      tinyIncome = 15000 * tinyOwned;
      tinyCost = 5000 * tinyOwned;
      
      console.log(tinyCost);
      document.getElementById('tiny-owned').innerHTML = nFormatter(tinyOwned);
      document.getElementById('tiny-income').innerHTML = "Income : $ " + nFormatter(tinyIncome);
      document.getElementById('tiny-cost').innerHTML = "Next Cost : $ " + nFormatter(tinyCost);
      document.getElementById('currentProfit').innerHTML = "Profit : $ " + nFormatter(gameProfit);
     }
    }
    tinyGamePlay();
<p id="tiny-owned"></p>
<p id="tiny-income"></p>
<p id="tiny-cost"></p>
<p id="currentProfit"></p>
Roman Habibi
  • 604
  • 5
  • 8