how do i print float=13.99 to 13 dollars and 99 cents ?

+2 md farzad · June 26, 2015
write a c programme to read the currency in decimal form and print dollar and cent sepperately

Post a Reply

Replies

Oldest  Newest  Rating
0 Kenneth Malicay · June 26, 2015
dollar = floor(amount);
cent = round((amount%1.0)*100);
+1 Kenneth Malicay · June 27, 2015
scratch that. I discovered % doesn't work on float so... here.
#include 
#include

int main(void) {
double amount = 128.72;
double dollar;
double cent;
cent = round(modf(amount, &dollar)*100);

printf("%.0f dollars and %.0f cents \n", dollar, cent);
return 0;
}
0 md farzad · June 29, 2015
thanks a lot  ^_^
0 md farzad · June 29, 2015
:happy:
+2 Jon Z. · July 9, 2015
here is another way.


#include

int main(void) {
double amount = 128.72;
int dollar = amount;
int cent = (amount - dollar) * 100 + 1; //+1, i'm not sure why it gives me 1 cent less otherwise.

printf("%d dollars and %d cents \n", dollar, cent);
return 0;
}
0 Rich Sharma · August 31, 2015
you could multiply by hundred and then use % and /= 10 to find the last two digits which are the cents. however this only works if the dollars and cents are more modern, and there isnt something like 13.995 dollars. (to be fair i dont think in this modern world you could pay for something with 99.5 cents)
  • 1

C

107,179 followers
About

One of the most popular languages of all time.

Links
Moderators
Bucky Roberts Administrator