Amount & PricedAmount are objects of CCurrency class (implementation is
part of my own project).
dblPrice is declared as double.
Implementation of CCurrency is as follows
---
class CCurrency
{
// Constructors
public:
CCurrency();
CCurrency(CURRENCY cySrc);
CCurrency(const CCurrency& curSrc);
CCurrency(const _variant_t & varSrc);
CCurrency(long nUnits, long nFractionalUnits);
CCurrency(__int64 int64Value);
// Attributes
public:
enum CCurrencyStatus
{
valid = 0,
invalid = 1, // Invalid currency (overflow, div 0, etc.)
null = 2, // Literally has no value
};
CURRENCY m_cur;
CCurrencyStatus m_status;
void SetStatus(CCurrencyStatus status);
CCurrencyStatus GetStatus() const;
// Operations
public:
const CCurrency& operator=(CURRENCY cySrc);
const CCurrency& operator=(const CCurrency& curSrc);
const CCurrency& operator=(const _variant_t & varSrc);
BOOL operator==(const CCurrency& cur) const;
BOOL operator!=(const CCurrency& cur) const;
BOOL operator<(const CCurrency& cur) const;
BOOL operator>(const CCurrency& cur) const;
BOOL operator<=(const CCurrency& cur) const;
BOOL operator>=(const CCurrency& cur) const;
// CCurrency math
CCurrency operator+(const CCurrency& cur) const;
CCurrency operator-(const CCurrency& cur) const;
const CCurrency& operator+=(const CCurrency& cur);
const CCurrency& operator-=(const CCurrency& cur);
CCurrency operator-() const;
CCurrency operator*(const CCurrency& cur) const;
CCurrency operator*(long nOperand) const;
CCurrency operator*(float nOperand) const;
CCurrency operator*(double nOperand) const;
CCurrency operator/(long nOperand) const;
CCurrency operator/(float nOperand) const;
CCurrency operator/(double nOperand) const;
const CCurrency& operator*=(long nOperand);
const CCurrency& operator/=(long nOperand);
const CCurrency& operator*=(float nOperand);
const CCurrency& operator/=(float nOperand);
const CCurrency& operator*=(double nOperand);
const CCurrency& operator/=(double nOperand);
operator CURRENCY() const;
operator _variant_t() const;
// CCurrency definition
void SetCurrency(long nUnits, long nFractionalUnits);
CCurrency Absolute();
};
---
Victor Bazarov wrote:
ab**********@hotmail.com wrote: I have a basic question related to datatypes.
I am trying to read a value using Microsoft's ADO recordset from a
field (lets call it 'Price') with datatype decimal(19,6) => 19 =
Precision, 6 = Scale
1) When I read this field into float datatype. I get a value 1.9000,
which is correct. But when I read its value in a double datatype I get
1.8999999761581.
Do you know how those numbers (19 and 6) relate to the significant
digits and the range of the number? Since MS ADO "recordset" is not
defined in C++, you have to tell us. Then together we can think why
you see what you see.
2) My second question is related to multiplication.
Should it cause any problem?
dblPrice =1.23333;
Amount = 91000000;
PricedAmount= dblPrice * Amount
PricedAmountshould have 112233030
But i am getting 112230300 (thus I am loosing precision).
"Losing". Well, how are 'dblPrice' and 'Amount' declared? Hos is
'PricedAmount' declared?
Here is the implementation of Multiplication operator of CCurrency
class
[..]
How is this relevant to your questions?
If you need an industrial-strength system for handling money, you
should _not_ use 'float' or even 'double'. You should implement
your own numeric representation based most likely on fixed-point
arithmetic with something like 5 digits after the decimal point.
Look on the web for 'fixed-point arithmetic' and you shall find.
V
--
Please remove capital As from my address when replying by mail