php 获取PHP中的数字总和

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3232511/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 09:05:38  来源:igfitidea点击:

Get the sum of digits in PHP

phpstringsum

提问by Leticia Meyere

How do I find the sum of all the digits in a number in PHP?

如何在PHP中找到数字中所有数字的总和?

回答by Artefacto

array_sum(str_split($number));

This assumes the number is positive (or, more accurately, that the conversion of $numberinto a string generates only digits).

这假设数字是正数(或者,更准确地说,转换$number为字符串只生成数字)。

回答by NikiC

Artefactos method is obviously unbeatable, but here an version how one could do it "manually":

Artefactos 方法显然是无与伦比的,但这里有一个版本如何“手动”做到这一点:

$number = 1234567890;
$sum = 0;
do {
    $sum += $number % 10;
}
while ($number = (int) $number / 10);

This is actually faster than Artefactos method (at least for 1234567890), because it saves two function calls.

这实际上比 Artefactos 方法更快(至少对于1234567890),因为它节省了两次函数调用。

回答by BrunoLM

Another way, not so fast, not single line simple

另一种方式,不是那么快,不是单行简单

<?php

    $n = 123;
    $nstr = $n . "";

    $sum = 0;
    for ($i = 0; $i < strlen($nstr); ++$i)
    {
        $sum += $nstr[$i];
    }
    echo $sum;

?>

It also assumes the number is positive.

它还假定该数字为正数。

回答by Mayank Dudakiya

Try the following code:

试试下面的代码:

<?php

$num = 525;
$sum = 0;

while ($num > 0)
{
    $sum= $sum + ($num % 10);
    $num= $num / 10;
}
echo "Summation=" . $sum;

?>

回答by itsazzad

If interested with regex:

如果对正则表达式感兴趣:

array_sum(preg_split("//", $number));

回答by Chandan Baruah

Assume you want to find the sum of the digits of a number say 2395 the simplest solution would be to first split the digits and find out the sum then concatenate all the numbers into one single number.

假设你想找到一个数字的数字总和,比如 2395,最简单的解决方案是首先拆分数字并找出总和,然后将所有数字连接成一个数字。

<?php
    $number=2;
    $number1=3;
    $number2=9;
    $number3=5;
    $combine=$number.$number1.$number2.$number3;
    $sum=$number+$number1+$number2+$number3;
    echo "The sum of $combine is $sum";
     ?>

回答by Ashutosh Verma

    <?php
echo"----Sum of digit using php----";
echo"<br/ >";
$num=98765;
$sum=0;
$rem=0;
for($i=0;$i<=$num;$i++)
{
$rem=$num%10;
$sum=$sum+$rem;
$num=$num/10;
}
echo "The sum of digit 98765 is ".$sum;
?>
-----------------Output-------------
----Sum of digit using php----
The sum of digit 98765 is 35

回答by stereoIII6

// math before code 

// base of digit sums is 9 

// the product of all numbers multiplied by 9 equals 9 as digit sum

$nr = 58821.5712; // any number

// Initiallization 

$d = array();

$d = explode(".",$nr); // cut decimal digits

$fl = strlen($d[1]); // count decimal digits

$pow = pow(10 ,$fl); // power up for integer

$nr = $nr * $pow; // make float become integer

// The Code

$ds = $nr % 9; // modulo of 9 

if($ds == 0) $ds=9; // cancel out zeros

echo $ds;

回答by Aries

function addDigits($num) {

    if ($num % 9 == 0 && $num > 0) {
        return 9;
    } else {
          return $num % 9;
        }
    }

only O(n)

只有 O(n)

at LeetCode submit result: Runtime: 4 ms, faster than 92.86% of PHP online submissions for Add Digits. Memory Usage: 14.3 MB, less than 100.00% of PHP online submissions for Add Digits.

在 LeetCode 提交结果: 运行时间:4 毫秒,比 92.86% 的 PHP 在线提交添加数字快。内存使用:14.3 MB,不到 PHP 在线提交的 Add Digits 的 100.00%。

回答by user2446600

<?php 
// PHP program to calculate the sum of digits 
function sum($num) { 
    $sum = 0; 
    for ($i = 0; $i < strlen($num); $i++){ 
        $sum += $num[$i]; 
    } 
    return $sum; 
} 

// Driver Code 
$num = "925"; 
echo sum($num); 
?> 

Result will be 9+2+5 = 16

结果将是 9+2+5 = 16