【问题标题】:Inventory product computation and conversion库存产品计算和转换
【发布时间】:2017-11-22 03:05:22
【问题描述】:

我正在编码产品库存。现在我正在计算或将项目转换为更大的单元。

例如: 产品A:1盒=12瓶

用户在添加交易时可以输入3盒13瓶这样的数据。产品 A 的新值将是 3 boxes and 13 bottles in storage。数据将保存到数据库tbl_transaction

我怎样才能像4 boxes and 1 bottle in storage这样将项目作为一个整体自动翻转/转换以添加到我的tbl_storage中?

这个公式我试过了,但是瓶数在小数点后恐怕不准确。

$bottles = 13;
$box = 12; 
$remaining_in_bottle = number_format($bottles / $box);// this will only convert the bottle into box (also tried float but not sure what I am doing)
$total_box = 3 + ???; 

echo $total_box." boxes and ".$remaining_in_bottle ." bottles in storage

【问题讨论】:

    标签: php floating-point decimal


    【解决方案1】:

    我假设用户只输入数字作为 boxesbottles 的值,但如果不是,您只需在执行以下计算之前从字符串中提取这些值:

    代码:(Demo)

    $bottles_per_box=12; 
    
    $user_input_bottles=13;
    $user_input_boxes=3;
    
    if($user_input_bottles>$bottles_per_box){
        // add truncated integer to box count.  DO NOT USE ROUND(), USE FLOOR()
        $user_input_boxes+=floor($user_input_bottles/$bottles_per_box);
    
        // store remainder after division
        $user_input_bottles=$user_input_bottles%$bottles_per_box;
        //                                     ^-- modulo operator
    }
    
    echo "New Bottles Total: $user_input_bottles\n";
    echo "New Boxes Total: $user_input_boxes\n";
    

    输出:

    New Bottles Total: 1
    New Boxes Total: 4
    

    【讨论】:

    • 感谢您的意见和解决方案。非常简单。干杯!我希望我知道在获取余数时如何使用%
    • 余数的结果有可能是十进制吗?
    • 使用floor() 时,您实际上是从数字中删除了任何十进制值。 1.9 变为 12.1 变为 2。这就是为什么需要floor()round() 将提供错误结果的原因。使用modulo operator 时,您将只寻找剩余值。例如13/121 的余数(不能使用小数)。
    • 太棒了!谢谢。
    【解决方案2】:

    我假设您想为 tbl_transactiontbl_storage 输入不同的内容。

    代码

    //Max bottle per box
    $box_max_bottles = 12;
    
    //User Input
    $input_box = 3; 
    $input_bottle = 13;
    
    //Transaction
    $transaction  = (($input_box > 1) ? $input_box . ' boxes' : $input_box . ' box') 
                    . ' and ' . (($input_bottle > 1) ? $input_bottle. ' bottles' : $input_bottle. ' bottle'). ' in storage';
    
    //Data will save into database tbl_transaction
    echo $transaction;
    
    //Get the remainder which is the remaining bottle
    $total_bottle = $input_bottle % 12;
    
    //Get the total boxes and divide the bottle into 12 
    $total_box = floor($input_box + ($input_bottle / 12));
    
    echo "<br />";
    
    //Storage
    $storage  = (($total_box > 1) ? $total_box . ' boxes' : $total_box . ' box') 
                    . ' and ' . (($total_bottle > 1) ? $total_bottle . ' bottles' : $total_bottle . ' bottle'). ' in storage';
    
    //Data will save into database tbl_storage
    echo $storage;
    

    输出

    交易

    3 boxes and 13 bottles in storage
    

    存储

    4 boxes and 1 bottle in storage
    

    【讨论】:

    • 感谢您的回答!对此,我真的非常感激。你和 mickmackusa 有正确的解决方案,但我接受 mickmackusa 的答案和解决方案,因为它更适用于我的情况。谢谢。
    • 说实话,这不是一个正确的方法——至少因为round()我没有测试代码,但是round()是不对的。请阅读我的答案并更新您的答案。
    • @mickmackusa 你是对的。我读了你的答案,应该改用floor(),因为round() 将在 .5+ 时四舍五入,并且将来可能会产生不正确的输出。没注意到。感谢您指出这一点。
    猜你喜欢
    • 1970-01-01
    • 2012-12-22
    • 2014-04-21
    • 1970-01-01
    • 1970-01-01
    • 2023-03-09
    • 1970-01-01
    • 2011-11-27
    • 1970-01-01
    相关资源
    最近更新 更多