Last active
January 16, 2018 13:49
-
-
Save ivanbogomoloff/5cf8b5ef42ab42a1ca164e3bc6fd0def to your computer and use it in GitHub Desktop.
Php sum from two string that over 65535 int
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$str = '11000000000000000000000 33'; | |
//echo 11000000000000000000000+33 will produce 1.1E+22983411000000000000000000030 | |
//but with this code you will get 11000000000000000000033 | |
$e = explode(" ", $str); | |
$num1 = $e[0]; | |
$num2 = $e[1]; | |
//100 | |
// 10 | |
// or | |
//10 | |
//100 | |
$lenNum1 = strlen($num1); | |
$lenNum2 = strlen($num2); | |
if($lenNum1 < $lenNum2) { | |
$tmp = $num2; | |
$num2 = $num1; | |
$num1 = $tmp; | |
} | |
$cur = 0; | |
$offset = strlen($num1) - strlen($num2); | |
$res = ''; | |
$prev = 0; | |
$num2 = str_split($num2); | |
foreach (str_split($num1) as $k => $sym) { | |
$intNum = (int) $sym; | |
$intNum2 = 0; | |
if ($cur >= $offset && isset($num2[$k-$offset])) { | |
$intNum2 = (int) $num2[$k-$offset]; | |
} | |
$s = $intNum + $intNum2; | |
if ($s >= 10) { | |
$res = (int) $prev + 1; | |
$s = $s - 10; | |
} | |
$res .= $s; | |
$prev = $res; | |
$cur++; | |
} | |
echo $res . "\n"; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment