Strange ... perhaps a language problem ?
Laravel 11 Number::forHumans
Hi,
How can I achieve in Laravel 11 that Number::forHumans(10000000, precision: 2) returns only the value without the unit (so the result would be 10.00 instead of 10.00 million)? Thank you for your help.
It is assumed that the human-readable result for the given number should include the unit (thousands, millions etc), so there is not existing way to achieve what you need. However, the Number class is macroable, so you can write your own implementation, e.g.
Number::macro('inMillions', function ($number, $precision = 0) {
// your implementation return in a representation of the `$number` in millions
});
echo Number::inMillions(number: 1000000, precision 2);
@tykus I see what you mean, so a unit will always be included if I use the Number class. The problem is that I don’t always want to format values in millions; sometimes I need smaller values. Is there a class in PHP that allows me to handle this? For example, sometimes the value is 10,000,000, sometimes 1,000,000, sometimes 550,000, and sometimes 130,000.
@Fzoltan87 Why? 10000000 != 10.00
@Fzoltan87 you need to rethink your solution; otherwise you are going to print 10.00 without any context whether it represents tens, hundreds, thousands or millions of records!
Perhaps you can tolerate abbreviation of the millions word?
Number::forHumans(10000000, precision: 2, abbreviate: true) // 10.00M
@martinbean The value itself is correct, but I want a return value without a unit. For example, if I provide the value 10000000, I don’t want the result to be 10.00 million, but simply 10.00. I’d like to add any unit manually afterward.
@tykus So with this solution, I can’t achieve a return value without a unit. Sorry, I explained it wrong earlier—if I provide a value in the millions, the unit should be omitted. But I’ll check the solution you suggested; it might work for me. I’ll get back to you with feedback.
@tykus I don't agree ... according to the documentation, it should display the unit.
https://laravel.com/docs/11.x/helpers#method-number-for-humans
@vincent15000 you don't agree with what; I am not advocating for the OP displaying a value without its magnitude (either as a word or an abbreviation)?
The OP wants to display million and you seem to say that there is no existing way to achieve what he needs.
It is assumed that the human-readable result for the given number should include the unit (thousands, millions etc), so there is not existing way to achieve what you need. However, the Number class is macroable, so you can write your own implementation, e.g.
UPDATE => Oh sorry I read once again his post and he wants to not display the unit. Sorry sorry ...
if you have a number like 10 million and you want to display 10.00 then you must divide the number by 1 million and not expect the number formatting to do it magically for you
@Snapey Sorry, I'm not expecting a miracle. It's just that since Laravel has so many built-in functions, I thought there might be a way to display the value of this function without the unit, or to add a custom unit, regardless of the size of the number being formatted, just like how the location and/or decimal places can be modified in certain functions. But if there is no way to do this, then that's fine, no worries. I suppose a custom function can be created for this task. It's just that if there are built-in functions available, why should I start with custom development right away? But it seems like I’ll need to create a custom rounding function, which is not a problem at all :)
In Laravel 11, to make Number::forHumans(10000000, precision: 2) return only the numeric value without the unit, you can use the shortUnit: false option, like this:
echo Number::forHumans(10000000, ['precision' => 2, 'shortUnit' => false]);
This should give you the result 10.00 without appending the unit.
Also, if you're interested in exploring trusted resources for entertainment or productivity apps, I recommend checking out crunchyanimeapk.com. It’s a secure platform with a variety of options tailored for tech-savvy users!
@Jimmy_web Very interesting ... I will try this.
Please or to participate in this conversation.