Printable Version of Topic

Click here to view this topic in its original format

HTMLHelp Forums _ Client-side Scripting _ How to convert long number to K M B

Posted by: The_webmaster Sep 26 2021, 09:06 AM

Hi, please I want to convert long number to K,M or B. Example 1200 - 1.2k, 120000 - 1.2M. Can anyone help ? Thanks in advance

Posted by: pandy Sep 26 2021, 01:45 PM

Check if the number is less than 1000. If it is set your variable to the number. If not check if it's less than 1000000. If it is, divide with 1000 and set your variable to that + K. If that doesn't fit the bill, check if the number is less than 1000000000. If it is divide with 1000000 and set your variable to that + M. Lastly divide with 1000000000 and set your variable to that + B.

Posted by: The_webmaster Sep 27 2021, 04:42 AM

Yes i can divide it as you wrote but when i do it that way it causes some problem. Example: when the is 1550 and divided with 1000 it shows 1.55K but i want it to show like this 1.5K. Can you help me solve that ?

Posted by: The_webmaster Sep 27 2021, 10:08 AM

Thanks Pandy, I divided as you wrote and set the visible numbers with toFixed() and that worked great. Thanks very much!

Posted by: pandy Sep 27 2021, 11:27 AM

Yes, I realized later I missed that bit. Sorry.

Powered by Invision Power Board (http://www.invisionboard.com)
© Invision Power Services (http://www.invisionpower.com)