Advertisement
Namokonov

suminwords

May 19th, 2022
778
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2. Таблица с примером https://docs.google.com/spreadsheets/d/1c68bh_y0PV6FN-h1u6N-NJteb4-9b359I4nqWAJw7NU/edit#gid=0
  3.  
  4. ✅ Канал о Таблицах: https://t.me/google_sheets
  5. ✅ Чат: https://t.me/google_spreadsheets_chat
  6. ✅ Оглавление канала: https://goo.gl/HdS2qn
  7. */
  8.  
  9. function words(input) {
  10.   var a, b, c, d, e, f, output, outputA, outputB, outputC, outputD, outputE, outputF;
  11.   var ones = ['', 'ONE', 'TWO', 'THREE', 'FOUR', 'FIVE', 'SIX', 'SEVEN', 'EIGHT', 'NINE'];
  12.   if (input === 0) { // Zero
  13.     output = "ZERO DOLLARS";
  14.   } else if (input == 1) { // One
  15.     output = "ONE DOLLAR ONLY";
  16.   } else { // More than one
  17.     input1 = Math.floor(input)
  18.     // Tens
  19.     a = input1 % 100;
  20.     outputA = oneToHundred_(a);
  21.     // Hundreds
  22.     b = Math.floor((input1 % 1000) / 100);
  23.     if (b > 0 && b < 10) {
  24.       outputB = ones[b];
  25.     }
  26.     // Thousands
  27.     c = (Math.floor(input1 / 1000)) % 100;
  28.     outputC = oneToHundred_(c);
  29.  
  30.     // Millions
  31.     d = (Math.floor(input1 / 100000)) % 100;
  32.     outputD = oneToHundred_(d);
  33.  
  34.     // Billions
  35.     e = (Math.floor(input1 / 10000000)) % 100;
  36.     outputE = oneToHundred_(e);
  37.  
  38.     // Cents
  39.     f = Math.round((input - input1) * 100);
  40.     outputF = f;
  41.  
  42.     output = "USD";
  43.  
  44.     if (a > 0) {
  45.       output = outputA;
  46.     }
  47.  
  48.     if (b > 0) {
  49.       output = outputB + " HUNDRED " + output;
  50.     }
  51.     if (c > 0) {
  52.       output = outputC + " THOUSAND " + output;
  53.     }
  54.     if (d > 0) {
  55.       output = outputD + " MILLION " + output;
  56.     }
  57.     if (e > 0) {
  58.       output = outputE + " BILLION " + output;
  59.     }
  60.     output = output + " DOLLARS";
  61.     if (f > 0) {
  62.       output = output + " AND " + outputF + " CENTS"
  63.     }
  64.  
  65.   }
  66.   return output;
  67. }
  68.  
  69. function oneToHundred_(num) {
  70.   var outNum;
  71.   var ones = ['', 'ONE', 'TWO', 'THREE', 'FOUR', 'FIVE', 'SIX', 'SEVEN', 'EIGHT', 'NINE'];
  72.   var teens = ['TEN', 'ELEVEN', 'TWELVE', 'THIRTEEN', 'FOURTEEN', 'FIFTEEN', 'SIXTEEN', 'SEVENTEEN', 'EIGHTEEN', 'NINETEEN'];
  73.   var tens = ['', '', 'TWENTY', 'THIRTY', 'FORTY', 'FIFTY', 'SIXTY', 'SEVENTY', 'EIGHTY', 'NINETY'];
  74.   if (num >= 0 && num < 10) { // 1 to 9
  75.     outNum = ones[num]; // ones
  76.   } else if (num > 9 && num < 20) { // 10 to 19
  77.     outNum = teens[(num % 10)]; // teens
  78.   } else if (num > 19 && num < 100) { // 20 to 100
  79.     outNum = tens[Math.floor(num / 10)]; // tens
  80.     if (num % 10 > 0) {
  81.       outNum = outNum + " " + ones[num % 10]; // tens + ones
  82.     }
  83.   }
  84.   return outNum;
  85. }
Advertisement
RAW Paste Data Copied
Advertisement