Answer the question
In order to leave comments, you need to log in
How to decompose a mass written in kg into all units of measurement?
Good day!
There is a table like this:
The base unit of measurement in it is the kilogram.
There is a formula
$quantity = 12514.1212134545342; // КГ
$units = массив с данными из таблицы.
function fullNameUnit($quantity,$units) {
$res = array();
foreach ($units as $unit) {
if ($quantity==0) { break; }
$res[$unit['code']] = rtrim(floor($quantity / $unit['base_unit_quantity']));
$quantity = $quantity % $unit['base_unit_quantity'];
}
return $res;
}
dd($quantity . ' кг', fullNameUnit($quantity,$data);
Answer the question
In order to leave comments, you need to log in
// $quantity = $quantity % $unit['base_unit_quantity'];
$quantity -= $res[$unit['code']] * $unit['base_unit_quantity'];
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question