The easiest way to make it work with negatives is probably to check if the number is negative at the beginning. If so, make it positive and set a flag. Then run through the code you have, and at the end check the flag and if it's set, prepend a minus sign to the result.
--Mark