php numbers like (10M, ..)

泪湿孤枕 提交于 2019-12-13 11:20:19

问题


I would like to work with numbers like 10M (which represents 10 000 000), and 100K etc. Is there a function that already exists in PHP, or do I have to write my own function?

I'm thinking something along the lines of :

echo strtonum("100K"); // prints 100000

Also, taking it one step further and doing the opposite, something to translate and get 100K from 100000?


回答1:


You could whip up your own function, because there isn't an builtin function for this. To give you an idea:

function strtonum($string)
{
    $units = [
        'M' => '1000000',
        'K' => '1000',
    ];

    $unit = substr($string, -1);

    if (!array_key_exists($unit, $units)) {
        return 'ERROR!';
    }

    return (int) $string * $units[$unit];
}

Demo: http://codepad.viper-7.com/2rxbP8

Or the other way around:

function numtostring($num)
{
    $units = [
        'M' => '1000000',
        'K' => '1000',
    ];

    foreach ($units as $unit => $value) {
        if (is_int($num / $value)) {
            return $num / $value . $unit;
        }
    }   
}

Demo: http://codepad.viper-7.com/VeRGDs


If you want to get really funky you could put all that in a class and let it decide what conversion to run:

<?php

class numberMagic
{
    private $units = [];

    public function __construct(array $units)
    {
        $this->units = $units;
    }

    public function parse($original)
    {
        if (is_numeric(substr($original, -1))) {
            return $this->numToString($original);
        } else {
            return $this->strToNum($original);
        }
    }

    private function strToNum($string)
    {
        $unit = substr($string, -1);

        if (!array_key_exists($unit, $this->units)) {
            return 'ERROR!';
        }

        return (int) $string * $this->units[$unit];
    }

    private function numToString($num)
    {
        foreach ($this->units as $unit => $value) {
            if (is_int($num / $value)) {
                return $num / $value . $unit;
            }
        }   
    }
}

$units = [
    'M' => 1000000,
    'K' => 1000,
];
$numberMagic = new NumberMagic($units);
echo $numberMagic->parse('100K'); // 100000
echo $numberMagic->parse(100); // 100K

Although this may be a bit overkill :)

Demo: http://codepad.viper-7.com/KZEc7b




回答2:


For second one

<?php
    #    Output easy-to-read numbers
    #    by james at bandit.co.nz
    function bd_nice_number($n) {
        // first strip any formatting;
        $n = (0+str_replace(",","",$n));

        // is this a number?
        if(!is_numeric($n)) return false;

        // now filter it;
        if($n>1000000000000) return round(($n/1000000000000),1).' trillion';
        else if($n>1000000000) return round(($n/1000000000),1).' billion';
        else if($n>1000000) return round(($n/1000000),1).' million';
        else if($n>1000) return round(($n/1000),1).' thousand';

        return number_format($n);
    }
?>


来源:https://stackoverflow.com/questions/14360282/php-numbers-like-10m

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!