38 #include "rational.hpp"
41 template<
class T=
long,
int N=2>
65 template<
class U,
int M>
69 template<
class U,
int M>
85 template<
class Char,
class Traits>
86 bool read(std::basic_istream<Char, Traits>& strm);
149 template<
class T,
int N>
153 std::ostringstream digits;
154 digits.imbue(std::locale::classic());
155 digits << integer() << std::setw(value_type::places) << std::setfill(
'0') << fraction();
157 std::ostringstream out;
158 std::money_put<char>
const& put(std::use_facet<std::money_put<char> >(std::locale()));
159 put.put(std::ostreambuf_iterator<char>(out),
false, out,
'0', digits.str());
163 template<
class T,
int N>
170 template<
class T,
int N>
177 template<
class T,
int N>
183 template<
class T,
int N>
190 template<
class T,
int N>
196 template<
class T,
int N>
203 template<
class T,
int N>
209 template<
class T,
int N>
216 template<
class T,
int N>
224 template<
class T,
int N>
231 template<
class T,
int N>
239 template<
class T,
int N>
240 template<
class CharT,
class Traits>
244 typename std::basic_istream<CharT, Traits>::sentry sentry(strm,
false);
248 std::ios_base::iostate error(std::ios_base::goodbit);
250 std::money_get<CharT>
const&
get(
251 std::use_facet<std::money_get<CharT> >(strm.getloc()));
252 get.get(std::istreambuf_iterator<CharT>(strm), std::istreambuf_iterator<CharT>(),
253 false, strm, error, digits);
255 if ((error & std::ios_base::failbit) != 0)
258 std::moneypunct<CharT>
const& punct(
259 std::use_facet<std::moneypunct<CharT> >(strm.getloc()));
262 std::string fraction(digits.substr(digits.size() - punct.frac_digits(), punct.frac_digits()));
264 std::string integer(digits.substr(0, digits.size() - punct.frac_digits()));
266 std::istringstream fixed_stream(integer +
"." + fraction);
267 return value_.read(fixed_stream);
273 template<
class T,
int N,
class Char,
class Traits>
276 if (not c.
read(strm))
277 strm.setstate(strm.failbit);
284 template<
class T,
int N,
class Char,
class Traits>
285 std::basic_ostream<Char, Traits>& operator<<(std::basic_ostream<Char, Traits>& strm,
currency<T,N> c)
287 typename std::basic_ostream<Char, Traits>::sentry sentry(strm);
288 strm << c.as_string();
293 template<
class T,
int N>
303 template<
class T,
int N>
313 template<
class T,
int N>
323 template<
class T,
int N>
333 template<
class T,
int N>
343 template<
class T,
int N>
353 template<
class T,
int N>
365 template<
class T,
int N>
373 template<
class T,
int N>
376 return a.
value() == b;
381 template<
class T,
int N>
384 return a == b.
value();
390 template<
class T,
int N>
398 template<
class T,
int N>
406 template<
class T,
int N>
415 template<
class T,
int N>
423 template<
class T,
int N>
424 bool operator<(currency<T,N> a, T b)
426 return a.value() < b;
431 template<
class T,
int N>
432 bool operator<(T a, currency<T,N> b)
434 return a < b.value();
440 template<
class T,
int N>
448 template<
class T,
int N>
456 template<
class T,
int N>
465 template<
class T,
int N>
473 template<
class T,
int N>
474 inline bool operator<=(currency<T,N> a, T b)
481 template<
class T,
int N>
482 inline bool operator<=(T a, currency<T,N> b)
490 template<
class T,
int N>
498 template<
class T,
int N>
506 template<
class T,
int N>
currency< T, N > operator+(currency< T, N > a, currency< T, N > b)
currency & operator+=(currency c)
currency & operator--()
Pre-decrement operator.
bool read(std::basic_istream< Char, Traits > &strm)
Implement a fixed-point number class template. Values have N places after the decimal point...
bool operator==(currency< T, N > a, currency< T, N > b)
currency< T, N > operator*(currency< T, N > a, T b)
constexpr currency()
Default constructor initializes the value to zero.
fixed< T, N > value_type
Type of the actual value.
value_type value() const
Return the internal value.
currency< T, N > operator/(currency< T, N > a, T b)
std::string as_string() const
Save and restore I/O stream flags.
bool operator!=(currency< T, N > a, currency< T, N > b)
value_type fraction() const
Return the fractional part, e.g., 3 for 12.03.
int_type round() const
Round off to the nearest integer, using banker's rounding.
constexpr currency(T integer)
Represent a rational number (fraction) as a numerator and denominator.
void negate()
Negate this value.
currency(currency< U, M > const &rhs)
Copy a value that uses a different precision.
currency & operator=(currency< U, M > rhs)
Assign a value that uses a different precision.
currency & operator*=(value_type m)
bool operator>=(currency< T, N > a, currency< T, N > b)
currency & operator-=(currency c)
currency(T integer, T fraction)
int_type integer() const
Return the integer part (which is the same as trunc()).
int_type fraction() const
Return the fractional part, to M places.
currency & operator++()
Pre-increment operator.
value_type integer() const
Return the integer part (which is the same as trunc()).
value_type value() const
Return the internal value.
bool operator>(currency< T, N > a, currency< T, N > b)
int_type fraction() const
Return the fractional part.
U convert() const
Convert to some other type, especially floating point.
std::basic_istream< Char, Traits > & operator>>(std::basic_istream< Char, Traits > &strm, currency< T, N > &c)
currency & operator/=(value_type m)
currency< T, N > operator-(currency< T, N > a)
Negate a currency value.