separating dollars and cents
How does one separate dollars and cents?
For instance, if I write a program that outputs, $5.36 how can I make it read,
Your total change is 5 dollars and 37 cents.
Thanks!
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
|
#include <iostream>
int main() {
const int CENTS_PER_DOLLAR = 100 ;
const double amount = 5.36 ;
const int amount_in_cents = amount * CENTS_PER_DOLLAR + 0.5 ; // narrow to int
const int dollars = amount_in_cents / CENTS_PER_DOLLAR ;
const int cents = amount_in_cents % CENTS_PER_DOLLAR ;
std::cout << "Your total change is " << dollars << " dollars and "
<< cents << " cents.\n" ;
}
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
|
#include <iostream>
int main()
{
const int CENTS_PER_DOLLAR = 100;
const double money = 5.36;
const int dollars = money;
const int cents = (money - dollars) * CENTS_PER_DOLLAR;
std::cout << "For $" << money << ":\n\n";
std::cout << dollars << " dollars and ";
std::cout << cents << " cents.\n";
}
|
Topic archived. No new replies allowed.