-1

How can i convert a String to an Integer without losing the zeros and preventing an overflow.

<?php
$iban = "DE85370100500123456403";

$d = 13;
$e = 14;
$pruefz = substr($iban, 2, 2);
$blz = substr($iban, 4, 8);
$kto = substr($iban, 12, 10);
$eine0 = 0;
$zweite0 = 0;
$zuBerZahl = ($blz . $kto . $d . $e. $eine0 . $zweite0);
$rest = bcmod($zuBerZahl, 97);
$pruefziffer = 98 - $rest;

echo "d =  $d  <br>";
echo "e =  $e  <br>";
echo "pruefz =  $pruefz  <br>";
echo "blz =  $blz  <br>";
echo "kto =  $kto  <br>";
echo "eine0 =  $eine0  <br>";
echo "zweite0 =  $zweite0  <br>";
echo "zuBerZahl =  $zuBerZahl  <br>";
echo "rest =  $rest  <br>";
echo "pruefziffer =  $pruefziffer  <br>";

This has solved my problem which I posted too before:

<?php
$iban = "DE85370100500123456503";

$d = 13;
$e = 14;
$pruefz = substr($iban, 2, 2);
$blz = substr($iban, 4, 8);
$kto = substr($iban, 12, 10);
$eine0 = 0;
$zweite0 = 0;
$zuBerZahl = $blz . $kto .  $d . $e . $eine0 . $zweite0;
$result = bcadd($zuBerZahl,'0', 0);
$rest = bcmod($result, 97);
$pruefziffer = 98 - $rest;


echo $pruefziffer;

With this PHP can now calculate with larger numbers

IT-noob
  • 31
  • 2
  • https://stackoverflow.com/questions/20983339/validate-iban-php ? – Alex K. Mar 02 '23 at 12:23
  • "Losing" what zeros? If you are talking about the _leading_ zeros in a value such as the `kto = 0123456403` your code outputs - you can't. _Real_ numbers never have leading zeros. Only when you _format_ such values for displaying them _as strings_, you can have such leading zeroes. – CBroe Mar 02 '23 at 12:33
  • And such a bank account number like `kto = 0123456403` _is_ not a number to begin with. Same as telephone or house "numbers" aren't. – CBroe Mar 02 '23 at 12:35

2 Answers2

2

Thank you for your answers and your kind disslikes.

This is how the code works now:

    <?php
$iban = "DE85370100500123456503";

$d = 13;
$e = 14;
$pruefz = substr($iban, 2, 2);
$blz = substr($iban, 4, 8);
$kto = substr($iban, 12, 10);
$eine0 = 0;
$zweite0 = 0;
$zuBerZahl = $blz . $kto .  $d . $e . $eine0 . $zweite0;
$result = bcadd($zuBerZahl,'0', 0);
$rest = bcmod($result, 97);
$pruefziffer = 98 - $rest;


echo $pruefziffer;
IT-noob
  • 31
  • 2
  • 1
    unless this is the solution to your own question, you should add it as an update to your previous question. – mw509 Mar 02 '23 at 12:55
  • @mw509 I don't know how it's work... This website is overloaded for me – IT-noob Mar 02 '23 at 22:00
  • 1
    I understand. It can be overwhelming for a first encounter. You can use the edit link/button at the bottom of your first post. – mw509 Mar 02 '23 at 22:53
  • @mw509 sorry... I tried but I can't get it to work... don't find the website user friendly. I press on the code icon to be able to add codes and when I paste them into the field it is not recognized as a code.... – IT-noob Mar 03 '23 at 12:04
  • 1
    paste first, highlight the code and click the code icon. – mw509 Mar 03 '23 at 12:06
0

You can use the sprintf() function`

sprintf('%08d', 1234);
Output: 01234

in this example. The %08d format string specifies that the value to be formatted is an integer %d and that it should be padded with leading zeros to a total width of 8 characters %08.

You can change the 08 to about any reasonable number.

Am not sure why you want this but ideally, integers will not have leading 0s. supposing you want to do some math, you can do it with the real integers and after that add back the 0's but to actually have it in an equation does not make sense(from my point of view). unless it was a floating number that comes after the decimal point.

Ideally you can write a helper function or something that just prepends the 0s for you based on the logic of how the 0s appear

mw509
  • 1,957
  • 1
  • 19
  • 25