Loading...
Searching...
No Matches
ledger::amount_t Class Reference

Encapsulate infinite-precision commoditized amounts. More...

#include <amount.h>

Inherits ordered_field_operators< amount_t, ordered_field_operators< amount_t, double, ordered_field_operators< amount_t, unsigned long, ordered_field_operators< amount_t, long > > > >.

+ Collaboration diagram for ledger::amount_t:

Public Types

typedef uint_least16_t precision_t
 The amount's decimal precision.
 

Public Member Functions

Assignment and copy
 amount_t (const amount_t &amt)
 Copy an amount object.
 
 amount_t (const amount_t &amt, const annotation_t &details)
 Copy an amount object, applying the given commodity annotation details afterward.
 
amount_toperator= (const amount_t &amt)
 Assign an amount object.
 
amount_toperator= (const double val)
 
amount_toperator= (const unsigned long val)
 
amount_toperator= (const long val)
 
amount_toperator= (const string &str)
 
amount_toperator= (const char *str)
 
Comparison
int compare (const amount_t &amt) const
 Compare two amounts, returning a number less than zero if amt is greater, exactly zero if they are equal, and greater than zero if amt is less.
 
bool operator== (const amount_t &amt) const
 Test two amounts for equality.
 
template<typename T >
bool operator== (const T &val) const
 
template<typename T >
bool operator< (const T &amt) const
 
template<typename T >
bool operator> (const T &amt) const
 
Binary arithmetic
amount_toperator+= (const amount_t &amt)
 
amount_toperator-= (const amount_t &amt)
 
amount_toperator*= (const amount_t &amt)
 
amount_tmultiply (const amount_t &amt, bool ignore_commodity=false)
 
amount_toperator/= (const amount_t &amt)
 Divide two amounts while extending the precision to preserve the accuracy of the result.
 
Unary arithmetic
precision_t precision () const
 Return an amount's internal precision.
 
bool keep_precision () const
 
void set_keep_precision (const bool keep=true) const
 
precision_t display_precision () const
 
amount_t negated () const
 Returns the negated value of an amount.
 
void in_place_negate ()
 
amount_t operator- () const
 
amount_t abs () const
 Returns the absolute value of an amount.
 
amount_t inverted () const
 
void in_place_invert ()
 
amount_t rounded () const
 Yields an amount whose display precision when output is truncated to the display precision of its commodity.
 
void in_place_round ()
 
amount_t roundto (int places) const
 
void in_place_roundto (int places)
 
amount_t truncated () const
 Yields an amount which has lost all of its extra precision, beyond what the display precision of the commodity would have printed.
 
void in_place_truncate ()
 
amount_t floored () const
 Yields an amount which has lost all of its extra precision, beyond what the display precision of the commodity would have printed.
 
void in_place_floor ()
 
amount_t ceilinged () const
 Yields an amount which has lost all of its extra precision, beyond what the display precision of the commodity would have printed.
 
void in_place_ceiling ()
 
amount_t unrounded () const
 Yields an amount whose display precision is never truncated, even though its commodity normally displays only rounded values.
 
void in_place_unround ()
 
amount_t reduced () const
 reduces a value to its most basic commodity form, for amounts that utilize "scaling commodities".
 
void in_place_reduce ()
 
amount_t unreduced () const
 unreduce(), if used with a "scaling commodity", yields the most compact form greater than one.
 
void in_place_unreduce ()
 
optional< amount_tvalue (const datetime_t &moment=datetime_t(), const commodity_t *in_terms_of=NULL) const
 Returns the historical value for an amount – the default moment returns the most recently known price – based on the price history for the given commodity (or determined automatically, if none is provided).
 
optional< amount_tprice () const
 
Truth tests
int sign () const
 Truth tests.
 
 operator bool () const
 
bool is_nonzero () const
 
bool is_zero () const
 
bool is_realzero () const
 
bool is_null () const
 
Conversion
double to_double () const
 Conversion methods.
 
long to_long () const
 
bool fits_in_long () const
 
 operator string () const
 
string to_string () const
 
string to_fullstring () const
 
string quantity_string () const
 
Commodity methods
commodity_tcommodity_ptr () const
 The following methods relate to an amount's commodity:
 
commodity_tcommodity () const
 
bool has_commodity () const
 
void set_commodity (commodity_t &comm)
 
amount_t with_commodity (const commodity_t &comm) const
 
void clear_commodity ()
 
amount_t number () const
 
Commodity annotations
void annotate (const annotation_t &details)
 An amount's commodity may be annotated with special details, such as the price it was purchased for, when it was acquired, or an arbitrary note, identifying perhaps the lot number of an item.
 
bool has_annotation () const
 
annotation_tannotation ()
 
const annotation_tannotation () const
 
amount_t strip_annotations (const keep_details_t &what_to_keep) const
 If the lot price is considered whenever working with commoditized values.
 
Printing
void print (std::ostream &out, const uint_least8_t flags=0x00) const
 
Debugging
void dump (std::ostream &out) const
 There are two methods defined to help with debugging:
 
bool valid () const
 

Static Public Member Functions

static void initialize ()
 Ready the amount subsystem for use.
 
static void shutdown ()
 Shutdown the amount subsystem and free all resources.
 

Static Public Attributes

static const std::size_t extend_by_digits = 6U
 Number of places of precision by which values are extended to avoid losing precision during division and multiplication.
 
static bool is_initialized
 
static bool stream_fullstrings
 If amounts should be streamed using to_fullstring() rather than to_string(), so that complete precision is always displayed no matter what the precision of an individual commodity may be.
 

Protected Member Functions

void _clear ()
 
void _copy (const amount_t &amt)
 
void _dup ()
 
void _release ()
 

Protected Attributes

commodity_tcommodity_
 
bigint_tquantity
 

Constructors

static amount_t exact (const string &value)
 Create an amount whose display precision is never truncated, even if the amount uses a commodity (which normally causes "round on streaming" to occur).
 
 amount_t ()
 Creates a value for which is_null() is true, and which has no value or commodity.
 
 amount_t (const double val)
 Convert a double to an amount.
 
 amount_t (const unsigned long val)
 Convert an unsigned long to an amount.
 
 amount_t (const long val)
 Convert a long to an amount.
 
 amount_t (const string &val)
 Parse a string as an (optionally commoditized) amount.
 
 amount_t (const char *val)
 Parse a pointer to a C string as an (optionally commoditized) amount.
 
 ~amount_t ()
 Release the reference count held for the underlying amount_t::bigint_t object.
 

Parsing

static void parse_conversion (const string &larger_str, const string &smaller_str)
 
bool parse (std::istream &in, const parse_flags_t &flags=PARSE_DEFAULT)
 The ‘flags’ argument of both parsing may be one or more of the following:
 
bool parse (const string &str, const parse_flags_t &flags=PARSE_DEFAULT)
 

Detailed Description

Encapsulate infinite-precision commoditized amounts.

Used to represent commoditized infinite-precision numbers, and uncommoditized, plain numbers. In the commoditized case, commodities keep track of how they are used, and are always displayed back to the user after the same fashion. For uncommoditized numbers, no display truncation is ever done. In both cases, internal precision is always kept to an excessive degree.

Definition at line 91 of file amount.h.

Member Typedef Documentation

◆ precision_t

The amount's decimal precision.

Definition at line 108 of file amount.h.

Constructor & Destructor Documentation

◆ amount_t() [1/8]

ledger::amount_t::amount_t ( )
inline

Creates a value for which is_null() is true, and which has no value or commodity.

If used in a value expression it evaluates to zero, and its commodity equals commodity_t::null_commodity.

Definition at line 137 of file amount.h.

References TRACE_CTOR.

Referenced by operator=(), operator=(), operator=(), operator=(), and operator=().

+ Here is the caller graph for this function:

◆ amount_t() [2/8]

ledger::amount_t::amount_t ( const double val)

Convert a double to an amount.

As much precision as possible is decoded from the binary floating point number.

◆ amount_t() [3/8]

ledger::amount_t::amount_t ( const unsigned long val)

Convert an unsigned long to an amount.

It's precision is zero.

◆ amount_t() [4/8]

ledger::amount_t::amount_t ( const long val)

Convert a long to an amount.

It's precision is zero, and the sign is preserved.

◆ amount_t() [5/8]

ledger::amount_t::amount_t ( const string & val)
inlineexplicit

Parse a string as an (optionally commoditized) amount.

If no commodity is present, the resulting commodity is commodity_t::null_commodity. The number may be of infinite precision.

Definition at line 156 of file amount.h.

References parse(), and TRACE_CTOR.

+ Here is the call graph for this function:

◆ amount_t() [6/8]

ledger::amount_t::amount_t ( const char * val)
inlineexplicit

Parse a pointer to a C string as an (optionally commoditized) amount.

If no commodity is present, the resulting commodity is commodity_t::null_commodity. The number may be of infinite precision.

Definition at line 164 of file amount.h.

References assert, parse(), and TRACE_CTOR.

+ Here is the call graph for this function:

◆ ~amount_t()

ledger::amount_t::~amount_t ( )
inline

Release the reference count held for the underlying amount_t::bigint_t object.

Definition at line 183 of file amount.h.

References _release(), quantity, and TRACE_DTOR.

+ Here is the call graph for this function:

◆ amount_t() [7/8]

ledger::amount_t::amount_t ( const amount_t & amt)
inline

Copy an amount object.

Copies are very efficient, using a copy-on-write model. Until the copy is changed, it refers to the same memory used by the original via reference counting. The amount_t::bigint_t class in amount.cc maintains the reference.

Definition at line 196 of file amount.h.

References _copy(), commodity_, quantity, and TRACE_CTOR.

+ Here is the call graph for this function:

◆ amount_t() [8/8]

ledger::amount_t::amount_t ( const amount_t & amt,
const annotation_t & details )
inline

Copy an amount object, applying the given commodity annotation details afterward.

This is equivalent to doing a normal copy (

See also
amount_t(const amount_t&)) and then calling amount_t::annotate().

Definition at line 207 of file amount.h.

References _copy(), annotate(), assert, quantity, and TRACE_CTOR.

+ Here is the call graph for this function:

Member Function Documentation

◆ _clear()

void ledger::amount_t::_clear ( )
protected

◆ _copy()

void ledger::amount_t::_copy ( const amount_t & amt)
protected

Referenced by amount_t(), and amount_t().

+ Here is the caller graph for this function:

◆ _dup()

void ledger::amount_t::_dup ( )
protected

◆ _release()

void ledger::amount_t::_release ( )
protected

Referenced by ~amount_t().

+ Here is the caller graph for this function:

◆ abs()

amount_t ledger::amount_t::abs ( ) const
inline

Returns the absolute value of an amount.

Equivalent to:

(x < * 0) ? - x : x
T & downcast(U &object)
Definition utils.h:468

Definition at line 323 of file amount.h.

References negated(), and sign().

+ Here is the call graph for this function:

◆ annotate()

void ledger::amount_t::annotate ( const annotation_t & details)

An amount's commodity may be annotated with special details, such as the price it was purchased for, when it was acquired, or an arbitrary note, identifying perhaps the lot number of an item.

annotate_commodity(amount_t price, [datetime_t date, string tag]) sets the annotations for the current amount's commodity. Only the price argument is required, although it can be passed as ‘none’ if no price is desired.

commodity_annotated() returns true if an amount's commodity has any annotation details associated with it.

annotation_details() returns all of the details of an annotated commodity's annotations. The structure returns will evaluate as boolean false if there are no details.

strip_annotations() returns an amount whose commodity's annotations have been stripped.

Referenced by amount_t().

+ Here is the caller graph for this function:

◆ annotation() [1/2]

annotation_t & ledger::amount_t::annotation ( )

Referenced by annotation().

+ Here is the caller graph for this function:

◆ annotation() [2/2]

const annotation_t & ledger::amount_t::annotation ( ) const
inline

Definition at line 613 of file amount.h.

References annotation().

+ Here is the call graph for this function:

◆ ceilinged()

amount_t ledger::amount_t::ceilinged ( ) const
inline

Yields an amount which has lost all of its extra precision, beyond what the display precision of the commodity would have printed.

Definition at line 375 of file amount.h.

References in_place_ceiling().

+ Here is the call graph for this function:

◆ clear_commodity()

void ledger::amount_t::clear_commodity ( )
inline

Definition at line 571 of file amount.h.

References commodity_.

Referenced by number().

+ Here is the caller graph for this function:

◆ commodity()

commodity_t & ledger::amount_t::commodity ( ) const
inline

Definition at line 552 of file amount.h.

References commodity_ptr().

+ Here is the call graph for this function:

◆ commodity_ptr()

commodity_t * ledger::amount_t::commodity_ptr ( ) const

The following methods relate to an amount's commodity:

commodity() returns an amount's commodity. If the amount has no commodity, the value returned is the ‘null_commodity’.

has_commodity() returns true if the amount has a commodity.

set_commodity(commodity_t) sets an amount's commodity to the given value. Note that this merely sets the current amount to that commodity, it does not "observe" the amount for possible changes in the maximum display precision of the commodity, the way that ‘parse’ does.

clear_commodity() sets an amount's commodity to null, such that has_commodity() afterwards returns false.

number() returns a commodity-less version of an amount. This is useful for accessing just the numeric portion of an amount.

Referenced by commodity().

+ Here is the caller graph for this function:

◆ compare()

int ledger::amount_t::compare ( const amount_t & amt) const

Compare two amounts, returning a number less than zero if amt is greater, exactly zero if they are equal, and greater than zero if amt is less.

This method is used to implement all of the other comparison methods.

Referenced by operator<(), operator==(), and operator>().

+ Here is the caller graph for this function:

◆ display_precision()

precision_t ledger::amount_t::display_precision ( ) const

◆ dump()

void ledger::amount_t::dump ( std::ostream & out) const
inline

There are two methods defined to help with debugging:

dump(ostream) dumps an amount to an output stream. There is little different from print(), it simply surrounds the display value with a marker, for example "AMOUNT($1.00)". This code is used by other dumping code elsewhere in Ledger.

valid() returns true if an amount is valid. This ensures that if an amount has a commodity, it has a valid value pointer, for example, even if that pointer simply points to a zero value.

Definition at line 737 of file amount.h.

References print().

+ Here is the call graph for this function:

◆ exact()

amount_t ledger::amount_t::exact ( const string & value)
inlinestatic

Create an amount whose display precision is never truncated, even if the amount uses a commodity (which normally causes "round on streaming" to occur).

This function is mostly used by debugging code and unit tests. This is the proper way to specify $100.005, where display of the extra digit precision is required. If a regular constructor were used, the amount would stream as $100.01, even though its internal value equals $100.005.

Definition at line 748 of file amount.h.

References parse(), ledger::PARSE_NO_MIGRATE, and value().

+ Here is the call graph for this function:

◆ fits_in_long()

bool ledger::amount_t::fits_in_long ( ) const

◆ floored()

amount_t ledger::amount_t::floored ( ) const
inline

Yields an amount which has lost all of its extra precision, beyond what the display precision of the commodity would have printed.

Definition at line 366 of file amount.h.

References in_place_floor().

+ Here is the call graph for this function:

◆ has_annotation()

bool ledger::amount_t::has_annotation ( ) const

◆ has_commodity()

bool ledger::amount_t::has_commodity ( ) const

Referenced by number().

+ Here is the caller graph for this function:

◆ in_place_ceiling()

void ledger::amount_t::in_place_ceiling ( )

Referenced by ceilinged().

+ Here is the caller graph for this function:

◆ in_place_floor()

void ledger::amount_t::in_place_floor ( )

Referenced by floored().

+ Here is the caller graph for this function:

◆ in_place_invert()

void ledger::amount_t::in_place_invert ( )

Referenced by inverted().

+ Here is the caller graph for this function:

◆ in_place_negate()

void ledger::amount_t::in_place_negate ( )

Referenced by negated().

+ Here is the caller graph for this function:

◆ in_place_reduce()

void ledger::amount_t::in_place_reduce ( )

Referenced by reduced().

+ Here is the caller graph for this function:

◆ in_place_round()

void ledger::amount_t::in_place_round ( )

Referenced by rounded().

+ Here is the caller graph for this function:

◆ in_place_roundto()

void ledger::amount_t::in_place_roundto ( int places)

Referenced by roundto().

+ Here is the caller graph for this function:

◆ in_place_truncate()

void ledger::amount_t::in_place_truncate ( )

Referenced by truncated().

+ Here is the caller graph for this function:

◆ in_place_unreduce()

void ledger::amount_t::in_place_unreduce ( )

Referenced by unreduced().

+ Here is the caller graph for this function:

◆ in_place_unround()

void ledger::amount_t::in_place_unround ( )

Referenced by unrounded().

+ Here is the caller graph for this function:

◆ initialize()

static void ledger::amount_t::initialize ( )
static

Ready the amount subsystem for use.

Note
Normally called by session_t::initialize().

◆ inverted()

amount_t ledger::amount_t::inverted ( ) const
inline

Definition at line 329 of file amount.h.

References in_place_invert().

+ Here is the call graph for this function:

◆ is_nonzero()

bool ledger::amount_t::is_nonzero ( ) const
inline

Definition at line 461 of file amount.h.

References is_zero().

Referenced by operator bool().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ is_null()

bool ledger::amount_t::is_null ( ) const
inline

Definition at line 470 of file amount.h.

References assert, commodity_, and quantity.

◆ is_realzero()

bool ledger::amount_t::is_realzero ( ) const
inline

Definition at line 466 of file amount.h.

References sign().

+ Here is the call graph for this function:

◆ is_zero()

bool ledger::amount_t::is_zero ( ) const

Referenced by is_nonzero().

+ Here is the caller graph for this function:

◆ keep_precision()

bool ledger::amount_t::keep_precision ( ) const

◆ multiply()

amount_t & ledger::amount_t::multiply ( const amount_t & amt,
bool ignore_commodity = false )

Referenced by operator*=().

+ Here is the caller graph for this function:

◆ negated()

amount_t ledger::amount_t::negated ( ) const
inline

Returns the negated value of an amount.

See also
operator-()

Definition at line 307 of file amount.h.

References in_place_negate().

Referenced by abs(), and operator-().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ number()

amount_t ledger::amount_t::number ( ) const
inline

Definition at line 575 of file amount.h.

References clear_commodity(), and has_commodity().

Referenced by quantity_string().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ operator bool()

ledger::amount_t::operator bool ( ) const
inline

Definition at line 458 of file amount.h.

References is_nonzero().

+ Here is the call graph for this function:

◆ operator string()

ledger::amount_t::operator string ( ) const
inline

Definition at line 518 of file amount.h.

References to_string().

+ Here is the call graph for this function:

◆ operator*=()

amount_t & ledger::amount_t::operator*= ( const amount_t & amt)
inline

Definition at line 276 of file amount.h.

References multiply().

+ Here is the call graph for this function:

◆ operator+=()

amount_t & ledger::amount_t::operator+= ( const amount_t & amt)

◆ operator-()

amount_t ledger::amount_t::operator- ( ) const
inline

Definition at line 314 of file amount.h.

References negated().

+ Here is the call graph for this function:

◆ operator-=()

amount_t & ledger::amount_t::operator-= ( const amount_t & amt)

◆ operator/=()

amount_t & ledger::amount_t::operator/= ( const amount_t & amt)

Divide two amounts while extending the precision to preserve the accuracy of the result.

For example, if 10 is divided by 3, the result ends up having a precision of amount_t::extend_by_digits place to avoid losing internal resolution.

◆ operator<()

template<typename T >
bool ledger::amount_t::operator< ( const T & amt) const
inline

Definition at line 260 of file amount.h.

References compare().

+ Here is the call graph for this function:

◆ operator=() [1/6]

amount_t & ledger::amount_t::operator= ( const amount_t & amt)

Assign an amount object.

This is like copying if the amount was null beforehand, otherwise the previous value's reference is must be freed.

◆ operator=() [2/6]

amount_t & ledger::amount_t::operator= ( const char * str)
inline

Definition at line 234 of file amount.h.

References amount_t(), and assert.

+ Here is the call graph for this function:

◆ operator=() [3/6]

amount_t & ledger::amount_t::operator= ( const double val)
inline

Definition at line 218 of file amount.h.

References amount_t().

+ Here is the call graph for this function:

◆ operator=() [4/6]

amount_t & ledger::amount_t::operator= ( const long val)
inline

Definition at line 224 of file amount.h.

References amount_t().

+ Here is the call graph for this function:

◆ operator=() [5/6]

amount_t & ledger::amount_t::operator= ( const string & str)
inline

Definition at line 231 of file amount.h.

References amount_t().

+ Here is the call graph for this function:

◆ operator=() [6/6]

amount_t & ledger::amount_t::operator= ( const unsigned long val)
inline

Definition at line 221 of file amount.h.

References amount_t().

+ Here is the call graph for this function:

◆ operator==() [1/2]

bool ledger::amount_t::operator== ( const amount_t & amt) const

Test two amounts for equality.

First the commodity pointers are quickly tested, then the multi-precision values themselves must be compared.

◆ operator==() [2/2]

template<typename T >
bool ledger::amount_t::operator== ( const T & val) const
inline

Definition at line 256 of file amount.h.

References compare().

+ Here is the call graph for this function:

◆ operator>()

template<typename T >
bool ledger::amount_t::operator> ( const T & amt) const
inline

Definition at line 264 of file amount.h.

References compare().

+ Here is the call graph for this function:

◆ parse() [1/2]

bool ledger::amount_t::parse ( const string & str,
const parse_flags_t & flags = PARSE_DEFAULT )
inline

Definition at line 682 of file amount.h.

References parse().

+ Here is the call graph for this function:

◆ parse() [2/2]

bool ledger::amount_t::parse ( std::istream & in,
const parse_flags_t & flags = PARSE_DEFAULT )

The ‘flags’ argument of both parsing may be one or more of the following:

PARSE_NO_MIGRATE means to not pay attention to the way an amount is used. Ordinarily, if an amount were $100.001, for example, it would cause the default display precision for $ to be "widened" to three decimal places. If PARSE_NO_MIGRATE is used, the commodity's default display precision is not changed.

PARSE_NO_REDUCE means not to call in_place_reduce() on the resulting amount after it is parsed.

These parsing methods observe the amounts they parse (unless PARSE_NO_MIGRATE is true), and set the display details of the corresponding commodity accordingly. This way, amounts do not require commodities to be pre-defined in any way, but merely displays them back to the user in the same fashion as it saw them used.

There is also a static convenience method called ‘parse_conversion’ which can be used to define a relationship between scaling commodity values. For example, Ledger uses it to define the relationships among various time values:

amount_t::parse_conversion("1.0m", "60s"); // a minute is 60 seconds
amount_t::parse_conversion("1.0h", "60m"); // an hour is 60 minutes
static void parse_conversion(const string &larger_str, const string &smaller_str)

The method parse() is used to parse an amount from an input stream or a string. A global operator>>() is also defined which simply calls parse on the input stream. The parse() method has two forms:

parse(istream, flags_t) parses an amount from the given input stream.

parse(string, flags_t) parses an amount from the given string.

parse(string, flags_t) also parses an amount from a string.

Referenced by amount_t(), amount_t(), exact(), ledger::operator>>(), and parse().

+ Here is the caller graph for this function:

◆ parse_conversion()

static void ledger::amount_t::parse_conversion ( const string & larger_str,
const string & smaller_str )
static

◆ precision()

precision_t ledger::amount_t::precision ( ) const

Return an amount's internal precision.

To find the precision it should be displayed at – assuming it was not created using amount_t::exact() – use the following expression instead:

amount.commodity().precision()

◆ price()

optional< amount_t > ledger::amount_t::price ( ) const

Referenced by ledger::price_point_t::operator==().

+ Here is the caller graph for this function:

◆ print()

void ledger::amount_t::print ( std::ostream & out,
const uint_least8_t flags = 0x00 ) const

Referenced by dump(), ledger::operator<<(), quantity_string(), to_fullstring(), and to_string().

+ Here is the caller graph for this function:

◆ quantity_string()

string ledger::amount_t::quantity_string ( ) const
inline

Definition at line 766 of file amount.h.

References number(), and print().

+ Here is the call graph for this function:

◆ reduced()

amount_t ledger::amount_t::reduced ( ) const
inline

reduces a value to its most basic commodity form, for amounts that utilize "scaling commodities".

For example, an amount of 1h after reduction will be 3600s.

Definition at line 395 of file amount.h.

References in_place_reduce().

+ Here is the call graph for this function:

◆ rounded()

amount_t ledger::amount_t::rounded ( ) const
inline

Yields an amount whose display precision when output is truncated to the display precision of its commodity.

This is normally the default state of an amount, but if one has become unrounded, this sets the "keep precision" state back to false.

See also
set_keep_precision

Definition at line 341 of file amount.h.

References in_place_round().

+ Here is the call graph for this function:

◆ roundto()

amount_t ledger::amount_t::roundto ( int places) const
inline

Definition at line 348 of file amount.h.

References in_place_roundto().

+ Here is the call graph for this function:

◆ set_commodity()

void ledger::amount_t::set_commodity ( commodity_t & comm)
inline

Definition at line 557 of file amount.h.

References commodity_, and quantity.

Referenced by with_commodity().

+ Here is the caller graph for this function:

◆ set_keep_precision()

void ledger::amount_t::set_keep_precision ( const bool keep = true) const

◆ shutdown()

static void ledger::amount_t::shutdown ( )
static

Shutdown the amount subsystem and free all resources.

Note
Normally called by session_t::shutdown().

◆ sign()

int ledger::amount_t::sign ( ) const

Truth tests.

An amount may be truth test in several ways:

sign() returns an integer less than, greater than, or equal to zero depending on whether the amount is negative, zero, or greater than zero. Note that this function tests the actual value of the amount – using its internal precision – and not the display value. To test its display value, use: ‘round().sign()’.

is_nonzero(), or operator bool, returns true if an amount's display value is not zero.

is_zero() returns true if an amount's display value is zero. Thus, $0.0001 is considered zero if the current display precision for dollars is two decimal places.

is_realzero() returns true if an amount's actual value is zero. Thus, $0.0001 is never considered realzero.

is_null() returns true if an amount has no value and no commodity. This only occurs if an uninitialized amount has never been assigned a value.

Referenced by abs(), and is_realzero().

+ Here is the caller graph for this function:

◆ strip_annotations()

amount_t ledger::amount_t::strip_annotations ( const keep_details_t & what_to_keep) const

If the lot price is considered whenever working with commoditized values.

Let's say a user adds two values of the following form:

10 AAPL + 10 AAPL {$20}

This expression adds ten shares of Apple stock with another ten shares that were purchased for $20 a share. If keep_price is false, the result of this expression is an amount equal to 20 AAPL. If keep_price is true the expression yields an exception for adding amounts with different commodities. In that case, a balance_t object must be used to store the combined sum.

◆ to_double()

double ledger::amount_t::to_double ( ) const

Conversion methods.

An amount may be converted to the same types it can be constructed from – with the exception of unsigned long. Implicit conversions are not allowed in C++ (though they are in Python), rather the following conversion methods must be called explicitly:

to_double([bool]) returns an amount as a double. If the optional boolean argument is true (the default), an exception is thrown if the conversion would lose information.

to_long([bool]) returns an amount as a long integer. If the optional boolean argument is true (the default), an exception is thrown if the conversion would lose information.

fits_in_long() returns true if to_long() would not lose precision.

to_string() returns an amount's "display value" as a string – after rounding the value according to the commodity's default precision. It is equivalent to: ‘round().to_fullstring()’.

to_fullstring() returns an amount's "internal value" as a string, without any rounding.

quantity_string() returns an amount's "display value", but without any commodity. Note that this is different from ‘number().to_string()’, because in that case the commodity has been stripped and the full, internal precision of the amount would be displayed.

◆ to_fullstring()

string ledger::amount_t::to_fullstring ( ) const
inline

Definition at line 760 of file amount.h.

References print(), and unrounded().

+ Here is the call graph for this function:

◆ to_long()

long ledger::amount_t::to_long ( ) const

◆ to_string()

string ledger::amount_t::to_string ( ) const
inline

Definition at line 754 of file amount.h.

References print().

Referenced by operator string().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ truncated()

amount_t ledger::amount_t::truncated ( ) const
inline

Yields an amount which has lost all of its extra precision, beyond what the display precision of the commodity would have printed.

Definition at line 357 of file amount.h.

References in_place_truncate().

+ Here is the call graph for this function:

◆ unreduced()

amount_t ledger::amount_t::unreduced ( ) const
inline

unreduce(), if used with a "scaling commodity", yields the most compact form greater than one.

That is, 3599s will unreduce to 59.98m, while 3601 unreduces to 1h.

Definition at line 406 of file amount.h.

References in_place_unreduce().

+ Here is the call graph for this function:

◆ unrounded()

amount_t ledger::amount_t::unrounded ( ) const
inline

Yields an amount whose display precision is never truncated, even though its commodity normally displays only rounded values.

Definition at line 384 of file amount.h.

References in_place_unround().

Referenced by ledger::operator<<(), and to_fullstring().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ valid()

bool ledger::amount_t::valid ( ) const

◆ value()

optional< amount_t > ledger::amount_t::value ( const datetime_t & moment = datetime_t(),
const commodity_t * in_terms_of = NULL ) const

Returns the historical value for an amount – the default moment returns the most recently known price – based on the price history for the given commodity (or determined automatically, if none is provided).

For example, if the amount were 10 AAPL, and on Apr 10, 2000 each share of AAPL was worth $10, then calling value() for that moment in time would yield the amount $100.00.

Referenced by exact().

+ Here is the caller graph for this function:

◆ with_commodity()

amount_t ledger::amount_t::with_commodity ( const commodity_t & comm) const
inline

Definition at line 562 of file amount.h.

References commodity_, and set_commodity().

+ Here is the call graph for this function:

Member Data Documentation

◆ commodity_

commodity_t* ledger::amount_t::commodity_
protected

Definition at line 128 of file amount.h.

Referenced by amount_t(), clear_commodity(), is_null(), set_commodity(), and with_commodity().

◆ extend_by_digits

const std::size_t ledger::amount_t::extend_by_digits = 6U
static

Number of places of precision by which values are extended to avoid losing precision during division and multiplication.

Definition at line 112 of file amount.h.

◆ is_initialized

bool ledger::amount_t::is_initialized
static

Definition at line 105 of file amount.h.

◆ quantity

bigint_t* ledger::amount_t::quantity
protected

Definition at line 127 of file amount.h.

Referenced by amount_t(), amount_t(), is_null(), set_commodity(), and ~amount_t().

◆ stream_fullstrings

bool ledger::amount_t::stream_fullstrings
static

If amounts should be streamed using to_fullstring() rather than to_string(), so that complete precision is always displayed no matter what the precision of an individual commodity may be.

Definition at line 117 of file amount.h.

Referenced by ledger::operator<<().


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