Project 3 - Currency Type
Public Types | Public Member Functions | List of all members
currency< T, N > Class Template Reference

#include <currency.hpp>

Public Types

typedef T int_type
 Storage type.
 
typedef fixed< T, N > value_type
 Type of the actual value.
 

Public Member Functions

constexpr currency ()
 Default constructor initializes the value to zero.
 
constexpr currency (T integer)
 
 currency (T integer, T fraction)
 
 currency (double value)
 
template<class U , int M>
 currency (currency< U, M > const &rhs)
 Copy a value that uses a different precision.
 
template<class U , int M>
currencyoperator= (currency< U, M > rhs)
 Assign a value that uses a different precision.
 
std::string as_string () const
 
template<class Char , class Traits >
bool read (std::basic_istream< Char, Traits > &strm)
 
template<class U >
convert () const
 Convert to some other type, especially floating point. More...
 
int_type round () const
 Round off to the nearest integer, using banker's rounding.
 
int_type integer () const
 Return the integer part (which is the same as trunc()).
 
template<int M>
int_type fraction () const
 Return the fractional part, to M places.
 
int_type fraction () const
 Return the fractional part.
 
currencyoperator+= (currency c)
 
currencyoperator-= (currency c)
 
currencyoperator*= (value_type m)
 
currencyoperator*= (int_type m)
 
currencyoperator/= (value_type m)
 
currencyoperator/= (int_type m)
 
void negate ()
 Negate this value.
 
currencyoperator++ ()
 Pre-increment operator.
 
currency operator++ (int)
 Post-increment operator.
 
currencyoperator-- ()
 Pre-decrement operator.
 
currency operator-- (int)
 Post-decrement operator.
 
value_type value () const
 Return the internal value.
 
template<class CharT , class Traits >
bool read (std::basic_istream< CharT, Traits > &strm)
 

Detailed Description

template<class T = long, int N = 2>
class currency< T, N >

Class to represent a currency value in the global locale.

Definition at line 42 of file currency.hpp.

Constructor & Destructor Documentation

template<class T = long, int N = 2>
constexpr currency< T, N >::currency ( integer)
inline

Initialize the value to integer

Parameters
integerThe integer initial value; the fractional part is zero.

Definition at line 52 of file currency.hpp.

template<class T = long, int N = 2>
currency< T, N >::currency ( integer,
fraction 
)
inline

Initialize the value. The interpretation of the fractional part depends on N. For example, if N is 2, a fraction of 9 represents 0.09, buf if N is 5, fraction of 9 means 0.0009.

Parameters
integerThe integer part of the initial value
fractionThe fractional part of the initial value

Definition at line 59 of file currency.hpp.

template<class T = long, int N = 2>
currency< T, N >::currency ( double  value)
inline

Initialize from a floating point number.

Parameters
valuethe initial value

Definition at line 62 of file currency.hpp.

Member Function Documentation

template<class T , int N>
std::string currency< T, N >::as_string ( ) const

Convert to a string.

Returns
a string representation of the value, e.g., "$123.04"

Definition at line 150 of file currency.hpp.

template<class T = long, int N = 2>
template<class U >
U currency< T, N >::convert ( ) const
inline

Convert to some other type, especially floating point.

Convert the value to a different numeric type. Typically, the other type is a floating-point type.

Definition at line 92 of file currency.hpp.

References currency< T, N >::value().

template<class T , int N>
currency< T, N > & currency< T, N >::operator*= ( value_type  m)

Multiplication operator.

Parameters
mthe value to multiply
Returns
*this

Definition at line 178 of file currency.hpp.

template<class T , int N>
currency< T, N > & currency< T, N >::operator*= ( int_type  m)

Multiplication operator.

Parameters
mthe value to multiply
Returns
*this

Definition at line 184 of file currency.hpp.

template<class T , int N>
currency< T, N > & currency< T, N >::operator+= ( currency< T, N >  c)

Addition operator.

Parameters
cthe value to add
Returns
*this

Definition at line 164 of file currency.hpp.

References currency< T, N >::value().

template<class T , int N>
currency< T, N > & currency< T, N >::operator-= ( currency< T, N >  c)

Subtraction operator.

Parameters
cthe value to subtract
Returns
*this

Definition at line 171 of file currency.hpp.

References currency< T, N >::value().

template<class T , int N>
currency< T, N > & currency< T, N >::operator/= ( value_type  m)

Division operator.

Parameters
mthe divisor
Returns
*this

Definition at line 191 of file currency.hpp.

template<class T , int N>
currency< T, N > & currency< T, N >::operator/= ( int_type  m)

Division operator.

Parameters
mthe divisor
Returns
*this

Definition at line 197 of file currency.hpp.

template<class T = long, int N = 2>
template<class Char , class Traits >
bool currency< T, N >::read ( std::basic_istream< Char, Traits > &  strm)

Overwrite this value with the value read from the stream. The value in the stream must have the correct number of digits. If the showbase flag is set, the currency symbol must be present.

Parameters
strmInput stream
Returns
true if the read is success and *this has been modified, or false if the read fails. Check strm for details.

Referenced by operator>>().


The documentation for this class was generated from the following file: