Handling monetary values as integers : Array Float « Data Type « C / ANSI-C






Handling monetary values as integers

#include <stdio.h>

int main()
{
  float amounts[5];
  long dollars[5];
  long cents[5];
  int i = 0;
  printf("Enter five monetary values separated by spaces:\n");

  for(i = 0 ; i<5 ; i++)
    scanf("%f", &amounts[i]);

  for(i = 0 ; i<5 ; i++)
  {
    dollars[i] = (long)amounts[i];
    cents[i] = (long)(100.0*(amounts[i]-dollars[i]));
  }

  printf("\n");
  for(i = 0 ; i<5 ; i++)
  {
    printf("  $%d.%02d", dollars[i], cents[i]);
  }
  printf("\n");
}

 

           
       








Related examples in the same category