#include <BigDecimal.hpp>
Public Member Functions | |
BigDecimal () | |
Empty constructor. | |
BigDecimal (const std::wstring &s) throw (ConversionException, UnexpectedException) | |
Constructs a BigDecimal which value is parsed from the given wstring (converted to a string) using createFromString() function. | |
BigDecimal (const std::string &s) throw (ConversionException, UnexpectedException) | |
Constructs a BigDecimal which value is parsed from the given string using createFromString() function. | |
BigDecimal (const int32_t) | |
Constructs a BigDecimal which value is taken from the given argument. | |
BigDecimal (const int64_t) | |
Constructs a BigDecimal which value is taken from the given argument. | |
BigDecimal (const BigDecimal &) | |
Copy constructor. | |
BigDecimal & | operator= (const BigDecimal &) |
Copies scale sign and unscaled_value. | |
bool | operator== (const BigDecimal &) const |
Compares scale, sign and unscaled_value. | |
BigDecimal (const DriverSocket &input) throw (SocketIOException, UnexpectedException) | |
Constructor to deserialize a BigDecimal from a stream. | |
virtual | ~BigDecimal () |
Frees intVal if applicable. | |
operator std::wstring () const | |
Conversion to string in the user defined locale (for decimal separator) A leading minus sign is used to indicate sign, and the number of digits to the right of the decimal point is used to indicate scale. | |
std::wstring | toString (const std::locale &loc) const |
Conversion to string with the given locale. | |
operator int32_t () const throw (ConversionException) | |
Conversion to int32. | |
operator int64_t () const throw (ConversionException) | |
Conversion to int64_t Any fractional part of this BigDecimal will be discarded, and if the resulting "BigInteger" is too big to fit in a int64_t, throws an exception. | |
operator uint64_t () const throw (ConversionException) | |
Conversion to uint64_t Any fractional part of this BigDecimal will be discarded, and if the resulting "BigInteger" is too big to fit in an uint64_t, throws an exception. | |
operator float () const throw (ConversionException) | |
Conversion to float TODO: exact behavior description. | |
operator double () const throw (ConversionException) | |
Conversion to double TODO: exact behavior description. | |
Protected Member Functions | |
void | initToZero () |
Initializes this big decimal to 0 (zero). | |
void | createFromString (const std::string &str) throw (ConversionException, UnexpectedException) |
Fills this BigDecimal fields (scale, signum, unscaled_value) by converting the given string into a BigDecimal, assuming it is base 10. | |
void | toBigInteger (mpz_t result) const |
Converts this BigDecimal's absolute value to a GMP integer by scaling its unscaled_value (thus discarding fractional part). | |
int * | toIntArray (mpz_t scaled_val, int *intArrayLength) const |
Converts the given gmp integer to an array of integers. | |
Friends | |
std::basic_ostream< wchar_t > & | operator<< (std::basic_ostream< wchar_t > &os, const BigDecimal &bd) |
BigDecimal display or serialization as a string with default C locale decimal point. |
A BigDecimal consists of an arbitrary precision integer unscaled value and a non-negative 32-bit integer scale, which represents the number of digits to the right of the decimal point. The number represented by the BigDecimal is (unscaledValue/10scale)
. One notable difference with Java is the unscaled value, that is not implemented with a BigInteger, but directly with an array of int (actually exactly as in BigInteger, minus utility functions the class provides)
CarobNS::BigDecimal::BigDecimal | ( | ) |
Empty constructor.
Sets fields to default values. This big decimal becomes zero (0 scale and 0 unscaled_value)
CarobNS::BigDecimal::BigDecimal | ( | const std::wstring & | s | ) | throw (ConversionException, UnexpectedException) |
Constructs a BigDecimal which value is parsed from the given wstring (converted to a string) using createFromString() function.
s | wide string to parse into a BigDecimal |
ConversionException | if the given string does not represent a valid BigDecimal |
CarobNS::BigDecimal::BigDecimal | ( | const std::string & | s | ) | throw (ConversionException, UnexpectedException) |
Constructs a BigDecimal which value is parsed from the given string using createFromString() function.
s | string to parse into a BigDecimal |
ConversionException | if the given string does not represent a valid BigDecimal |
CarobNS::BigDecimal::BigDecimal | ( | const DriverSocket & | input | ) | throw (SocketIOException, UnexpectedException) |
Constructor to deserialize a BigDecimal from a stream.
input | socket from which to deserializer Big Decimal |
SocketIOException | if an error occurs on the stream |
std::wstring CarobNS::BigDecimal::toString | ( | const std::locale & | loc | ) | const |
Conversion to string with the given locale.
Adds optionnal leading minus sign, decimal point and thousand separator. Decimal and thousand separators are taken from the given locale.
Note that the locale's grouping property is only used to determine if a grouping must be done. If so, grouping is assumed to be "\003" whatever the locale is (eg. 12345 will always be "12<sep>345").
This function is mandatory due to the internal representation that we use (full number + scale), which doesn't allow to use gmp's or stl's functions.
loc | the locale to be used for string conversion |
CarobNS::BigDecimal::operator int32_t | ( | ) | const throw (ConversionException) |
Conversion to int32.
Any fractional part of this BigDecimal will be discarded, and if the resulting "BigInteger" is too big to fit in an int32, throws an exception
ConversionException | if the value is to big to be converted |
CarobNS::BigDecimal::operator int64_t | ( | ) | const throw (ConversionException) |
Conversion to int64_t Any fractional part of this BigDecimal will be discarded, and if the resulting "BigInteger" is too big to fit in a int64_t, throws an exception.
ConversionException | if the value is to big to be converted |
CarobNS::BigDecimal::operator uint64_t | ( | ) | const throw (ConversionException) |
Conversion to uint64_t Any fractional part of this BigDecimal will be discarded, and if the resulting "BigInteger" is too big to fit in an uint64_t, throws an exception.
ConversionException | if the value is to big to be converted |
CarobNS::BigDecimal::operator float | ( | ) | const throw (ConversionException) |
Conversion to float TODO: exact behavior description.
ConversionException | if the value is to big to be converted |
CarobNS::BigDecimal::operator double | ( | ) | const throw (ConversionException) |
Conversion to double TODO: exact behavior description.
ConversionException | if the value is to big to be converted |
void CarobNS::BigDecimal::initToZero | ( | ) | [protected] |
Initializes this big decimal to 0 (zero).
There will be 0 signum, zero unscaled_value, zero scale, zero byte array length and byte array pointer set to NULL
void CarobNS::BigDecimal::createFromString | ( | const std::string & | str | ) | throw (ConversionException, UnexpectedException) [protected] |
Fills this BigDecimal fields (scale, signum, unscaled_value) by converting the given string into a BigDecimal, assuming it is base 10.
Finds the scale of the given number by parsing the input string in order to find the decimal point taken from the current locale. The scale is the number of digits found after the decimal point, ignoring white spaces.
str | string representing the number in base 10, that can contain white spaces (ignored). |
ConversionException | if the given string does not represent a valid BigDecimal in base 10 |
void CarobNS::BigDecimal::toBigInteger | ( | mpz_t | result | ) | const [protected] |
Converts this BigDecimal's absolute value to a GMP integer by scaling its unscaled_value (thus discarding fractional part).
result | the scaled value. Note: result must be initialized ! |
int* CarobNS::BigDecimal::toIntArray | ( | mpz_t | scaled_val, | |
int * | intArrayLength | |||
) | const [protected] |
Converts the given gmp integer to an array of integers.
The integer array will be allocated by this function, and must be deleted by the caller
scaled_val | gmp integer to convert (usually the return value of toBigInteger) | |
intArrayLength | size of the returned array |
std::basic_ostream<wchar_t>& operator<< | ( | std::basic_ostream< wchar_t > & | os, | |
const BigDecimal & | bd | |||
) | [friend] |
BigDecimal display or serialization as a string with default C locale decimal point.
This operator is used to send the bigdecimal as a string to the controller for parameter statements. The controller uses the default decimal point separator for parsing, so we have to send it the right one, which is the default C one.
os | stream in which to write the string representation of this BD | |
bd | value to be serialized |