Search code examples
laravellaravel-7

how to display 1000 as 1k in laravel 7


I'm new on La-ravel, i want to convert number value

from 1000 to 1k

I have searched documentation of Laravel but i couldn't find. Is there any function for that?


Solution

  • @Bharath-CODER put this function in your helper or create a private method inside of the controller.

    function getAmount($input)
        {
            $input = number_format($input);
            $input_count = substr_count($input, ',');
            if ($input_count != '0') {
                if ($input_count == '1') {
                    return substr($input, 0, -4) . 'k';
                } else if ($input_count == '2') {
                    return substr($input, 0, -8) . 'm';
                } else if ($input_count == '3') {
                    return substr($input, 0, -12) . 'b';
                } else {
                    return;
                }
            } else {
                return $input;
            }
        }
    

    Exampl value : 2k, 2m, 2b