2016-03-06 124 views
0

好的,所以我需要这个号码3.55687428096E+14实际上看起来像一个合适的整数。我不知道该怎么做才能让它看起来像一个整数,以便我可以继续使用我的编码。基本上,没有解决这个数字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

'number_format(3.55687428096E + 14,0,'','')' –

+0

好的,那么这是做什么的,我在哪里可以插入这段代码Anant? –

+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/>"; 
?> 
0

更改的最后一行有:

echo number_format($fact,0) ."<br/>"; 
+0

ok ......所以number_format正在工作,但我猜测不够充分。我想我需要采取马克贝克建议的第一件事。虽然不知道这是什么意思.... –

+0

它的工作和正确。它将输出355,687,428,096,000。这是你正在寻找的数字 –

0

@马克·贝克: - 我的代码下面这是成功运行了。感谢您的信息和支持:

<?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>"; 

?>