JavaScript-How to display the number of readings in PHP or JS friendly? For example, convert 1000 to 1k,10000 to 1w

Source: Internet
Author: User

More than 1000 of the number can be formatted as 1k
e.g. 1320 conversion to 1.3k
or 13200 conversion to 1.32w


effect, in the Internet for a long time, have not found this plug-in, which God can tell me where to find, or the name of such plug-in is what, thank you ~

Reply content:

More than 1000 of the number can be formatted as 1k
e.g. 1320 conversion to 1.3k
or 13200 conversion to 1.32w


effect, in the Internet for a long time, have not found this plug-in, which God can tell me where to find, or the name of such plug-in is what, thank you ~

function test($num){    if($num < 1000) {       return $num;    } else if($num >=1000 && $num < 10000){       return round($num/1000,1).'k';    } else if ($num >= 10000) {       return round($num/10000,2).'w';    }}echo test(1320);


  
   = 10000) {        $num = round($num / 10000 * 100) / 100 .' W';    } elseif($num >= 1000) {        $num = round($num / 1000 * 100) / 100 . ' K';    } else {        $num = $num;    }    return $num;}echo num2tring(12300); //1.23 Wecho num2tring(1200); //1.2 Kecho num2tring(200); //200

In JavaScript, the number is divided by 1000, then the result is rounded down with Math.floor, and the end is prefixed with K.
Math.floor (n/1000) + ' k ' K
PHP also has a similar usage

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.