All pastes #249002 Raw Edit

Unnamed

public text v1 · immutable
#249002 ·published 2006-11-15 07:51 UTC
rendered paste body
#include <stdlib.h>
#include <stdio.h>
#include <string.h>

char* sumtext( float sum ) {
      int pennies, dimes, ones, tens, hundreds;
      char sum_text[90] = "";
      char ones_text[][7] = {"One ", "Two ", "Three ", "Four ", "Five ", "Six ", "Seven ", "Eight ", "Nine "};
      char tens_text[][9] = {"Ten ", "Twenty ", "Thirty ", "Forty ", "Fifty ", "Sixty ", "Seventy ", "Eighty ", "Ninety "}; // set array of values here
      char teens_text[][11] = {"Eleven ", "Twelve ", "Thirteen ", "Fourteen ", "Fifteen ", "Sixteen ", "Seventeen ", "Eighteen ", "Nineteen "};
      hundreds = (int)(sum / 100);
      tens = (int)(sum / 10) - (hundreds * 10);
      ones = (int)sum - (hundreds * 100) - (tens * 10);
      dimes = (int)( ( sum - (int)sum ) * 10 );
      // round() or add .5 and then make int
      pennies = (int)((sum - (int)sum) * 100 - (dimes * 10));

      if ( hundreds > 0 ) {
           strcat(sum_text, ones_text[hundreds - 1]);
           strcat(sum_text, "Hundred ");
      }
      if ( tens == 1 && ones > 0) {
           strcat(sum_text, teens_text[ones - 1]);
      } else if ( tens > 0 ) {
           strcat(sum_text, tens_text[tens - 1]);
           if ( ones > 0 ) {
                strcat(sum_text, ones_text[ones - 1]);
           }
      } else {
           strcat(sum_text, ones_text[ones - 1]);                
      }
      strcat(sum_text, "Dollars And ");
      if ( dimes == 0 && pennies == 0 ) {
            strcat(sum_text, "No ");
      } else if ( dimes == 1 && pennies > 0) {
           strcat(sum_text, teens_text[pennies - 1]);
      } else if ( dimes > 0 ) {
           strcat(sum_text, tens_text[dimes - 1]);
           if ( ones > 0 ) {
                strcat(sum_text, ones_text[pennies - 1]);
           }
      } else {
           strcat(sum_text, ones_text[pennies - 1]);                
      }
      strcat(sum_text, "Cents");
      return sum_text;
}

int main(int argc, char *argv[]) {
    printf("%s\n", sumtext(115.25));
    printf("%s\n", sumtext(512.19));
    printf("%s\n", sumtext(520.10)); // floats are not stored as exact decimals... as fractions... so slightly off
    system("PAUSE");
    return EXIT_SUCCESS;
}