-2

65 + 10 = 75

75 x 15 x 686 = 771750

771750 / 365 / 30 = 70.479这是正确答案

这个代码显示答案错误

这是此代码74.400的错误答案

我该如何解决它请帮助我谢谢

$res = 65+10*15*686/365/30; //Adding the two values and placing the added result to 'res' variable

<?php //Starting of php 
    if(isset($_POST['submit']))//if the submit button has pressed
    {
    $days = $_POST['days']; //Getting Value of first integer from add.html
    $yeardays1 = $_POST['yeardays1']; //Getting Value of Second integer from add.html
    $yeardays = $_POST['yeardays1']; //Getting Value of Second integer from add.html
    $basicsalary = $_POST['basicsalary']; //Getting Value of Second integer from add.html
    $allowsalary = $_POST['allowsalary']; //Getting Value of Second integer from add.html
    $res = (($basicsalary + $allowsalary)* $days * $yeardays1) / $yeardays /$monthdays;


    echo number_format((round($res, 1)),3);

    }
    //Ending of php
    ?>
4

4 回答 4

4

您的运算符优先级逻辑是错误的,是时候回到一些基础数学(BODMAS)了。但是,对于您想要的优先级,您可以使用

<?php
        $res = ((65+10)*15*686)/365/30;
        echo 'Added Result:';

       echo number_format((round($res, 1)),3);

?>

添加结果:70.500

小提琴基于你的变量

于 2013-11-13T07:12:19.353 回答
0

你刚刚使用了round()函数,所以结果是正确的。在这里阅读 php.net/round

更改精度以获得更多位数。

现在我看到有更大的问题。表达式10*15*686/365/30将首先被评估,然后65将被添加。所以你提出的结果是不正确的。用来75*15*686/365/30得到你的结果。

于 2013-11-13T07:10:08.857 回答
0

你为什么使用roundAND number_format。选择其中之一。

<?php 
    $res = ( 65 + 10 ) * 15 * 686 / 365 / 30; //Adding the two values and placing the added result to 'res' variable
    echo 'Added Result:';

    echo number_format($res,3); /* 70.479 */
    //Ending of php
?>

.+ 你的算法是错误的。

于 2013-11-13T07:11:58.547 回答
0

Php 遵循我们在基础数学 BODMAS 中学习的相同规则

计算基于操作员的优先级

使用下面的代码

<?php
        $res = ((65+10)*15*686)/365/30;
        echo 'Added Result:';

       echo number_format((round($res, 1)),3);

?>
于 2013-11-13T07:15:32.503 回答