Question d’entretien chez Microsoft

I was asked to write a method which takes an integer as an input parameter and returned a string as the output variable. The string's value was to be the written format of the integer. For example, if the input was, "1,550,225" the output was, "One million, five hundred and fifty thousand, two hundred and twenty five"

Réponse à la question d'entretien

Utilisateur anonyme

5 avr. 2009

The trick was to break the routine down initially by keeping track of each place holder individually. This could be achieved by using the modulus operator, and starting with millions: numMills = myValue % 1000000 ... numHunThous = myValue % 100000, and so forth. The next part involved making an array of strings, which represented the written representation, and cancatanating a new string together, based on the numValues for each of the place holders in the input value. For example, if numMills == 0, we wouldn't want to say, "Zero Million, Five hundred thousand, etc, etc", we'd just skip over the millions. That's pretty much how the solution works, with a few other clauses for the grammatical structuring of the output string.