-1

I want to convert 1000000 to 1M also for 1000 To 1K

Any ideas?

I need it for my special project

Kyle Mods
  • 63
  • 1
  • 9
  • Change 1024 to 1000 if you want powers of 10 instead of 2 – mplungjan Jun 13 '18 at 11:43
  • I need a function – Kyle Mods Jun 13 '18 at 11:43
  • And also i dont want convert sizes, I want to convert numbers – Kyle Mods Jun 13 '18 at 11:44
  • 1
    Err... the answer in the dupe is a function and is easily converted to do what you want to do. Try putting in a bit of effort yourself – Pete Jun 13 '18 at 11:44
  • IM NEW TO JAVASCRIPT AND I DONT KNOW HOW TO MAKE THAT, IF I KNOW HOW TO DO IT WHY WOULD I ASK HERE? – Kyle Mods Jun 13 '18 at 11:46
  • Welcome to SO: No need to shout. If you are clueless and cannot convert the given function from 1024 to 1000, then you need https://www.upwork.com/ and not StackOverflow. In the answer in the dupe, change `var k = 1024,` to `var k = 1000,` – mplungjan Jun 13 '18 at 11:59

1 Answers1

6

You need to do the conversion from highest to lowest.

function convert(value)
{
    if(value>=1000000)
    {
        value=(value/1000000)+"M"
    }
    else if(value>=1000)
    {
        value=(value/1000)+"K";
    }
    return value;
}
console.log(convert(1000));
console.log(convert(10000));
console.log(convert(300000));
console.log(convert(3000000));
Vignesh Raja
  • 7,927
  • 1
  • 33
  • 42