DuxReiNummariae 1.0.0-alpha.23
Simple and powerful budgeting application
 
Loading...
Searching...
No Matches
drn::foundation::Percentage Class Reference

Represents a percentage value for use in mathematical calculations or user interfaces. More...

#include <Percentage.h>

Collaboration diagram for drn::foundation::Percentage:

Public Member Functions

 Percentage (const double rate=0.0, const quint8 precision=Percentage::defaultPrecision)
 Full initialisation constructor.
 
 operator double () const noexcept
 Conversion operator to the decimal representation of the percentage.
 
std::strong_ordering operator<=> (const double rate) const noexcept
 
std::strong_ordering operator<=> (const Percentage &other) const noexcept
 
bool operator== (const double rate) const noexcept
 
bool operator== (const Percentage &) const noexcept=default
 
quint8 precision () const noexcept
 An accessor to the number of digits to use in any operation involving the rate.
 
double rate () const noexcept
 An accessor to the percentage rate.
 

Static Public Attributes

static constexpr const quint8 defaultPrecision {6u}
 

Friends

DRN_FOUNDATION_EXPORT std::ostream & operator<< (std::ostream &out, const Percentage &p)
 Full object stream insertion operator.
 
DRN_FOUNDATION_EXPORT::QString presentationText (const Percentage &p)
 Generates a text representation for a percentage value.
 

Detailed Description

Represents a percentage value for use in mathematical calculations or user interfaces.

It is designed to simplify the representation and manipulation of fractional values expressed as percentages. It provides utility methods for converting between percentages and decimal values, as well as for formatting percentage values for display in graphical user interfaces or other textual outputs.

Constructor & Destructor Documentation

◆ Percentage()

drn::foundation::Percentage::Percentage ( const double rate = 0.0,
const quint8 precision = Percentage::defaultPrecision )
explicit

Full initialisation constructor.

Exceptions
ErrorWhen the rate is too large to truncate.
Parameters
rateThe percentage rate value. Any digits after the precision is truncated.
precisionThe number of digits after the decimal point.
Here is the call graph for this function:
Here is the caller graph for this function:

Member Function Documentation

◆ operator double()

drn::foundation::Percentage::operator double ( ) const
explicitnoexcept

Conversion operator to the decimal representation of the percentage.

Returns
The current percentage rate.

◆ operator<=>() [1/2]

std::strong_ordering drn::foundation::Percentage::operator<=> ( const double rate) const
nodiscardnoexcept
Here is the call graph for this function:

◆ operator<=>() [2/2]

std::strong_ordering drn::foundation::Percentage::operator<=> ( const Percentage & other) const
nodiscardnoexcept
Here is the call graph for this function:

◆ operator==() [1/2]

bool drn::foundation::Percentage::operator== ( const double rate) const
nodiscardnoexcept
Here is the call graph for this function:

◆ operator==() [2/2]

bool drn::foundation::Percentage::operator== ( const Percentage & ) const
nodiscarddefaultnoexcept
Here is the call graph for this function:

◆ precision()

quint8 drn::foundation::Percentage::precision ( ) const
nodiscardnoexcept

An accessor to the number of digits to use in any operation involving the rate.

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

◆ rate()

double drn::foundation::Percentage::rate ( ) const
nodiscardnoexcept

An accessor to the percentage rate.

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

Friends And Related Symbol Documentation

◆ operator<<

DRN_FOUNDATION_EXPORT std::ostream & operator<< ( std::ostream & out,
const Percentage & p )
friend

Full object stream insertion operator.

Parameters
outThe stream to place the data into.
pThe data to place into the stream.
Returns
The modified stream after insertion.

◆ presentationText

DRN_FOUNDATION_EXPORT::QString presentationText ( const Percentage & p)
friend

Generates a text representation for a percentage value.

This function creates a formatted string displaying the percentage value based on the rate and precision defined in the provided percentage object. It is useful for displaying percentage values in a user-readable format, such as in GUIs, logs, or reports.

Parameters
pAn object representing the percentage, containing the rate and precision.
Returns
A string representing the percentage value, appended with a percentage symbol.

Member Data Documentation

◆ defaultPrecision

const quint8 drn::foundation::Percentage::defaultPrecision {6u}
staticconstexpr

N.B. The number of decimal places is chosen because it seems to be good enough for the number of digits most percentage rates are advertised in.