I was going to do it in C but was confused, so I turned to PHP and was able to copy a recursive function to do this. I am converting an integer into a string with math. Here it is:
function intToString($myDecimal){
if($myDecimal < 10) {
return $myDecimal;
}
return intToString(($myDecimal / 10)) . ($myDecimal % 10);
}
I was able to convert a recursive factorial function before.. but with this I just have no clue.. My attempt is as follows:
function intToStringIter($myDecimal){
$out = "";
while($myDecimal > 10) {
$myDecimal /= 10;
$out .= $myDecimal;
}
$out .= $myDecimal % 10;
return $out;
}
I think I am too tired to see the proper logic at the moment.. It returns 22 instead of 20, I cannot wrap my head around what is correct. Do you see what I am doing wrong?
-
1Example of the usage please ?RobertPitt– RobertPitt2010年11月05日 09:43:56 +00:00Commented Nov 5, 2010 at 9:43
-
I can't find the point. Is this homework? Conversion between integer and strings isn't needed in PHP.xPheRe– xPheRe2010年11月05日 09:48:18 +00:00Commented Nov 5, 2010 at 9:48
-
@xPheRe, It was for an interview question a friend had asked about, he wasn't in the interview he just read about the question, I was helping him understand how to do it.John– John2010年11月05日 19:07:07 +00:00Commented Nov 5, 2010 at 19:07
2 Answers 2
If you're looking for a conversion to string for big unsigned integers, the code is actually:
function intToString($myDecimal)
{
return sprintf('%u', $myDecimal);
}
If you need to do it with iteration:
function intToString($myDecimal)
{
$result = '';
while ($myDecimal > 9) {
$result = ($myDecimal % 10) . $result;
$myDecimal /= 10;
}
return $myDecimal . $result;
}
UPDATE: My bad, digits were inserted in reversed order. Now it should work. Sorry, untested too.
-
I was wanting to do it in C, which used no standard functions (so sprintf is out), your second example spits out "02" instead of "20", weird. I am really just helping somebody do this in C, I will stop wracking my mind and go to sleep. :)John– John2010年11月05日 09:59:02 +00:00Commented Nov 5, 2010 at 9:59
-
I updated my answer, you were right, it built the string in reverse order. Now it should work fine. I hope.xPheRe– xPheRe2010年11月05日 10:02:18 +00:00Commented Nov 5, 2010 at 10:02
-
Ah! After some sleep I understand this now, it works perfectly and applies to the problem I wanted to help somebody with. It should be simple to do in C now (with simple ASCII addition). Thank youJohn– John2010年11月05日 19:04:44 +00:00Commented Nov 5, 2010 at 19:04
PHP is not very strict with variables. An integer will become an float if the situation likes it. In your code, $myDecimal /= 10
could make a float of $myDecimal
. The following forces $myDecimal to stay an integer. Note: you should pass only integers, if you're passing 9.99
, the output would still be 9.99
because 9.99 < 10
.
function intToStringIter($myDecimal){
$out = "";
while($myDecimal >= 10) {
$myDecimal = (int) ($myDecimal / 10);
$out .= $myDecimal;
}
$out .= $myDecimal % 10;
return $out;
}