closer01's avatar

How to convert 1000 to 1k ?

Hi, i am new on laravel, i want to convert number value from 1000 to 1k, i have seached documantation of laravel but i couldnt find.. is there any function for that?

0 likes
17 replies
goatshark's avatar

@closer01 Do you mean 1000 to 1k literally, or you want to convert numbers into their "k" equivalent? If the latter, I might use something like this:

    public function convert($value)
    {
        $number = $value / 1000;
        return $number . 'k';
    }
1 like
cviv's avatar

or you can refine @goatshark example

 public function convert($value)
    {
        $number = $value / 1000;
    //if you want 2 decimal digits
        return $newVal = number_format($number,2) . 'k';
    }
1 like
goatshark's avatar

@cviv Good call on that one. Depending on the source numbers ($value), this is likely a vital point to make.

1 like
Snapey's avatar

do you mean 1000 or 1024? Sorry for being pedantic.

1 like
jlrdw's avatar

do you mean 1000 or 1024? Sorry for being pedantic

Been talking to @jimmck ?

goatshark's avatar

@snapey That's awesome! I had three different replies written that included pointing out the 1000 vs. 1024 "thing". In an effort to not start a religious war, I opted to not point that out...... I only start religious wars on even numbered days.

It's worth thinking about though. There are legit times when 1000 is appropriate, and legit times when 1024 is appropriate.

I've always leaned in the 1024 direction myself.

Snapey's avatar

sure, lets not forget the conversion could be from metres to kilometres, and be entirely accurate.

jlrdw's avatar

@goatshark I'd write a helper class to do general conversion, unless all you need is the 1000 converted. Laravel / Taylor assumes the developer will write their own helper classes.

closer01's avatar

I mean i have database value $number and if it is more than 1000 i want to display in blade file like 1k, if it is 2500 want to display 2k etc... any example for this?

jlrdw's avatar

@closer01 kidding, right @cviv gave you an excellant example:

public function convert($value)
    {
        $number = $value / 1000;
    //if you want 2 decimal digits
        return $newVal = number_format($number,2) . 'k';
    }

But 2500 is not 2k

You need to look at the php manual on math functions, I will leave a hint: floor

sagar.jagtap's avatar

put this function in your helper or create private method inside of 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).'mil'; } else if($input_count == '3'){ return substr($input, 0, -12).'bil'; } else { return; } } else { return $input; } }

1 like
Chris1904's avatar

I personally use:

if ($count >= 1000) {
    return round($count/1000, 1) . "k";
} else {
    return $count;
}

That would write out every number less than a 1000 and, for example, if it is 2500, it would output 2.5k

3 likes

Please or to participate in this conversation.