IT-noob
IT-noob

Reputation: 31

Convert a String to an Integer with PHP

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

Upvotes: -1

Views: 131

Answers (2)

mw509
mw509

Reputation: 2093

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

Upvotes: 0

IT-noob
IT-noob

Reputation: 31

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;

Upvotes: 2

Related Questions