好的,所以我需要这个数字3.55687428096E+14
实际看起来像一个正确的整数。我不知道该怎么做才能使它看起来像一个整数,以便我可以继续我的编码。基本上,如果不解决这个数字3.55687428096E+14
,我就无法正常进行。以下是我的代码,请帮忙!
<?php
$num = 17;
$fact = $num;
echo "The factorial of $num is $num X ";
while ($num > 1){
--$num;
if ($num != 1){
echo "$num X ";
}
else{
echo "$num <b>equals</b> ";
}
$fact = $fact * $num;
}
echo $fact ."<br/>";
?>
顺便说一下,数字3.55687428096E+14
是我在运行程序后得到的结果。它可以直到阶乘16但是一旦变为17,事情变成3.55687428096E+14!
答案 0 :(得分:0)
如果您的号码非常大,您可以使用number_format()函数。
<强> number_format() 强>
number_format()函数格式化一个数字为千位的数字。
您的代码
<?php
$num = 17;
$fact = $num;
echo "The factorial of $num is $num X ";
while ($num > 1){
--$num;
if ($num != 1){
echo "$num X ";
}
else{
echo "$num <b>equals</b> ";
}
$fact = $fact * $num;
}
echo number_format($fact) ."<br/>";
?>
答案 1 :(得分:0)
使用以下内容更改最后一行
echo number_format($fact,0) ."<br/>";
答案 2 :(得分:0)
@Mark Baker: - 由于您的信息和支持,下面的代码已成功执行:
<?php
echo "<h2>Find the sum of the digits in the number 100!</h2>"."<br/>";
$num = 100;
$fact = $num;
echo "The factorial of $num is $num X ";
while ($num > 1){
--$num;
if ($num != 1){
echo " $num X ";
}
else{
echo " $num";
}
$fact = bcmul($fact,$num); //right over here!
}
echo " which equals" ."<br/>". "<h3>$fact</h3>";
//proceeding now to calculate the sum of the digits of the factorial!
$_array = str_split($fact);
$sum = array_sum($_array);
echo "The sum of the digits of the factorial is " ."<br/>";
echo "<h3>$sum</h3>";
&GT;