Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

format, iomanip, c++

Tags:

c++

iomanip

I'm trying to learn to use namespaces declarations more definitive than not just say "using namespace std". I'm trying to format my data to 2 decimal places, and set the format to be fixed and not scientific. This is my main file:

#include <iostream>
#include <iomanip>

#include "SavingsAccount.h"
using std::cout;
using std::setprecision;
using std::ios_base;

int main()
{
    SavingsAccount *saver1 = new SavingsAccount(2000.00);
    SavingsAccount *saver2 = new SavingsAccount(3000.00);

    SavingsAccount::modifyInterestRate(.03);

    saver1->calculateMonthlyInterest();
    saver2->calculateMonthlyInterest();

    cout << ios_base::fixed << "saver1\n" << "monthlyInterestRate: " << saver1->getMonthlyInterest()
        << '\n' << "savingsBalance: " << saver1->getSavingsBalance() << '\n';
    cout << "saver2\n" << "monthlyInterestRate: " << saver2->getMonthlyInterest()
        << '\n' << "savingsBalance: " << saver2->getSavingsBalance() << '\n';
}

On Visual Studio 2008, when I run my program, I get an output of "8192" before the data I want. Is there a reason for that?

Also, I don't think I am setting the fixed part or 2 decimal places correctly since I seem to get scientific notation once I added the setprecision(2). Thanks.

like image 922
Crystal Avatar asked Apr 28 '10 06:04

Crystal


2 Answers

You want std::fixed (the other one just inserts its value into the stream, which is why you see 8192), and I don't see a call to std::setprecision in your code anywhere.
This'll fix it:

#include <iostream>
#include <iomanip>

using std::cout;
using std::setprecision;
using std::fixed;

int main()
{
    cout << fixed << setprecision(2)
         << "saver1\n" 
         << "monthlyInterestRate: " << 5.5 << '\n' 
         << "savingsBalance: " << 10928.8383 << '\n';
    cout << "saver2\n" 
         << "monthlyInterestRate: " << 4.7 << '\n' 
         << "savingsBalance: " << 22.44232 << '\n';
}
like image 159
tzaman Avatar answered Oct 15 '22 11:10

tzaman


It might not be the answer you're looking for, but floating-point numbers are not suited to financial calculations because fractions like 1/100 cannot be represented exactly. You might be better off doing the formatting yourself. This can be encapsulated:

class money {
    int cents;
public:
    money( int in_cents ) : cents( in_cents ) {}

    friend ostream &operator<< ( ostream &os, money const &rhs )
        { return os << '$' << m.cents / 100 << '.' << m.cents % 100; }
};

cout << money( 123 ) << endl; // prints $1.23

Better(?) yet, C++ has a facility called the monetary locale category which includes a money formatter which takes cents as an argument.

locale::global( locale("") );
use_facet< money_put<char> >( locale() ).put( cout, false, cout, ' ', 123 );

This should Do the Right thing internationally, printing the user's local currency and hiding the number of decimal places from your implementation. It even accepts fractions of a cent. Unfortunately, this does not seem to work on my system (Mac OS X), which has generally poor locale support. (Linux and Windows should fare better.)

like image 3
Potatoswatter Avatar answered Oct 15 '22 12:10

Potatoswatter