Advertisement
Guest User

Untitled

a guest
Sep 18th, 2019
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.10 KB | None | 0 0
  1. public class Solution {
  2. private readonly string[] LESS_THAN_TWENTY = {"", "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten", "Eleven", "Twelve", "Thirteen", "Fourteen", "Fifteen", "Sixteen", "Seventeen", "Eighteen", "Nineteen"};
  3. private readonly string[] TENS = {"", "Ten", "Twenty", "Thirty", "Forty", "Fifty", "Sixty", "Seventy", "Eighty", "Ninety"};
  4. private readonly string[] THOUSANDS = {"", "Thousand", "Million", "Billion"};
  5. public string NumberToWords(int num) {
  6. if (num == 0) return "Zero";
  7. string word = string.Empty;
  8. int i = 0;
  9. while(num > 0) {
  10. if (num % 1000 > 0) {
  11. word = Helper(num %1000) + THOUSANDS[i] + " " + word;
  12. }
  13. num /= 1000;
  14. i++;
  15. }
  16. return word.Trim();
  17. }
  18.  
  19. private string Helper(int num) {
  20. if (num == 0) return "";
  21. else if (num < 20) return LESS_THAN_TWENTY[num] + " ";
  22. else if (num < 100) return TENS[num /10] + " " + Helper(num%10);
  23. return LESS_THAN_TWENTY[num/100] + " Hundred " + Helper(num%100);
  24. }
  25. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement