drby30217 2013-04-25 03:30
浏览 9
已采纳

可以用百分比而不是静态值来完成

Alright guys, Basically i've got some code which is working from an if statement, I'm wondering if it's possible to recode it to work from a percentage, here's the current code

@sort($prices_array);
if ($prices_array[1] > 10000 AND $prices_array[1] < 50000) {
    $sell_at = $prices_array[1] - 250;
} elseif ($prices_array[1] > 50000 AND $prices_array[1] < 100000) {
    $sell_at = $prices_array[1] - 500;
} else {
    $sell_at = $prices_array[1] - 100;
}
$buy_at = $prices_array[1] - 1000;

if ($prices_array[1] >= 10000) {
    $buy_at = $buy_at - 1000;
    if ($prices_array[1] >= 20000) {
        $buy_at = $buy_at - 1000;
        if ($prices_array[1] >= 30000) {
            $buy_at = $buy_at - 1000;
        }
        if ($prices_array[1] >= 40000) {
            $buy_at = $buy_at - 1000;
        }
        if ($prices_array[1] >= 50000) {
            $buy_at = $buy_at - 750;
        }
        if ($prices_array[1] >= 60000) {
            $buy_at = $buy_at - 500;
        }
        if ($prices_array[1] >= 70000) {
            $buy_at = $buy_at - 500;
        }
    }
}

I know the code is quite horribly done, but would it be possible to base the $buy_at from an certain percentage for example such as 5% lower than the sell_at but rounded to the correct decrements from the if statement

  • 写回答

1条回答 默认 最新

  • doucong7963 2013-04-25 04:10
    关注

    First of all, I think a switch would be nicer form here. Secondly, you can use this function to set up rounding to whatever increments you want:

    function roundUpTo($number, $increments) { 
        $increments = 1 / $increments; 
        return (ceil($number * $increments) / $increments); 
    }
    

    To find a percentage of a value, just multiply the value by the percentage divided by 100.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 划分vlan后不通了
  • ¥15 GDI处理通道视频时总是带有白色锯齿
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大