Help on String.int2String

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Help on String.int2String

pikachu
Here is my current code for String.int2String:


        function String int2String(int n) {
                var int lastDigit, i;
                var String output, tmp;
                var int c;
                if (n < 0) {
                        let output = String.new(10);
                        let output = output.appendChar(45);
                        let tmp = String.int2String(-n);
                        let i = 0;
                        while (i < tmp.length()) {
                                let output = output.appendChar(tmp[i]);
                        }
                } else {
                        let lastDigit = n - (n / 10 * 10);
                        let c = n + 48;
                        if (n < 10) {
                                let output = String.new(10);
                        } else {
                                let output = String.int2String(n / 10);
                        }
                        let output = output.appendChar(c);
                }
                return output;
        }

I've tested this, and an input of 12345 gives a string like (ascii integers): 49|60|[other numbers over 1000], and I don't know why and fixing it proves difficult.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Help on String.int2String

ivant
I see one problem with handling negative numbers. Hint: look at the while loop.

The code for the positive numbers looks correct. Are you using the "standard" String and other OS classes, or your own implementations? The problem could be in String.appendChar.
Loading...