2

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?

3
  • 1
    Example of the usage please ? Commented 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. Commented 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. Commented Nov 5, 2010 at 19:07

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

3 Comments

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. :)
I updated my answer, you were right, it built the string in reverse order. Now it should work fine. I hope.
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 you
0

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;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.