×

Welcome to TagMyCode

Please login or create account to add a snippet.
0
0
 
0
Language: ActionScript
Posted by: ccheek21 .
Added: May 19, 2016 11:33 PM
Views: 2002
  1. string int_to_words(long long n, bool use_and) {
  2.   string one_to_nine[] = {"","one","two","three","four","five","six","seven","eight","nine"};
  3.   string tens[] = {"","ten","twenty","thirty","forty","fifty","sixty","seventy","eighty","ninety"};
  4.   string teens[] = {"","eleven","twelve","thirteen","fourteen","fifteen","sixteen","seventeen","eighteen","nineteen"};
  5.   string thousands[] = {"","thousand","million","billion","trillion","quadrillion","quintillion","sextillion","septillion","octillion","nonillion"};
  6.  
  7.   int t = 1;
  8.  
  9.   while (pow(10,3*t) <= n ) {
  10.     t += 1;
  11.   }
  12.  
  13.   string return_string = "";
  14.  
  15.   for (; t > 0; t-- ) {
  16.     string string_t("");
  17.     int group_t = n % (long long)(pow(10,3*t)) / (long long)(pow(10,3*(t-1)));
  18.     if (group_t == 0) continue;
  19.  
  20.     int hundreds = group_t/100;
  21.     string hundreds_string("");
  22.     if (hundreds != 0){
  23.       hundreds_string = (one_to_nine[hundreds] + " hundred");
  24.     }
  25.  
  26.     int tens_and_ones = group_t % 100;
  27.     string tens_and_ones_string("");
  28.     if(tens_and_ones <= 9 && tens_and_ones > 0) {
  29.       tens_and_ones_string += one_to_nine[tens_and_ones];
  30.     } else if (tens_and_ones <= 19 && tens_and_ones >= 10) {
  31.       tens_and_ones_string += teens[tens_and_ones % 10];
  32.     } else if (tens_and_ones % 10 == 0) {
  33.       tens_and_ones_string += tens[tens_and_ones/10];
  34.     } else {
  35.       tens_and_ones_string += tens[tens_and_ones/10] + "-" + one_to_nine[tens_and_ones % 10];
  36.     }
  37.  
  38.     if (hundreds_string != "" && tens_and_ones_string != "") {
  39.       string_t = hundreds_string + (use_and ? " and " : " " ) + tens_and_ones_string + (t == 1 ? "" : " " ) + thousands[t-1] + ", ";
  40.     } else if (hundreds_string == "" && tens_and_ones_string == "" ) {
  41.       string_t = "";
  42.     } else {
  43.       string_t = hundreds_string + tens_and_ones_string + " " + thousands[t-1] + ", ";
  44.     }
  45.  
  46.     return_string += string_t;
  47.   }
  48.  
  49.   return_string.erase(return_string.end() - 2, return_string.end());
  50.   return return_string;
  51.  
  52.  
  53. }