CString用以转换一个decimal,为什么得到一个奇怪的值
CString用来转换一个decimal,为什么得到一个奇怪的值?
下面5行程序:
运行输出:
92559631349317831000000000000000000000000000000000000000000000.000000
为什么不是我期待的123,而是这么一个奇怪的数字呢?
谢谢。
------解决方案--------------------
strValue.Format(_T("%f"),d); 试下改为
------解决方案--------------------
这里d是一个结构体,另外lo64是long long类型,%f是对浮点型的。
------解决方案--------------------
先把DECIMAL 结构搞清楚
Represents a decimal data type that provides a sign and scale for a number (as in coordinates.)
Decimal variables are stored as 96-bit (12-byte) unsigned integers scaled by a variable power of 10. The power of 10 scaling factor specifies the number of digits to the right of the decimal point, and ranges from 0 to 28.
Syntax
JavaScriptC#C++F#JScriptVBView ColorizedCopy to Clipboard
typedef struct tagDEC {
USHORT wReserved;
union {
struct {
BYTE scale;
BYTE sign;
};
USHORT signscale;
};
ULONG Hi32;
union {
struct {
ULONG Lo32;
ULONG Mid32;
};
ULONGLONG Lo64;
};
} DECIMAL;
typedef struct tagDEC {
USHORT wReserved;
union {
struct {
BYTE scale;
BYTE sign;
};
USHORT signscale;
};
ULONG Hi32;
union {
struct {
ULONG Lo32;
ULONG Mid32;
};
ULONGLONG Lo64;
};
} DECIMAL;
Members
wReserved
Reserved.
scale
The number of decimal places for the number. Valid values are from 0 to 28. So 12.345 is represented as 12345 with a scale of 3.
sign
Indicates the sign; 0 for positive numbers or DECIMAL_NEG for negative numbers. So -1 is represented as 1 with the DECIMAL_NEG bit set.
signscale
The sign and number of decimal places.
Hi32
The high 32 bits of the number.
Lo32
The low 32 bits of the number.
Mid32
The middle 32 bits of the number.
Lo64
The low 64 bits of the number. This is an _int64.
下面5行程序:
DECIMAL d;
d.Lo64=123;
CString strValue;
strValue.Format(_T("%f"),d);
_tprintf(_T("%s\n"),strValue.GetBuffer());
运行输出:
92559631349317831000000000000000000000000000000000000000000000.000000
为什么不是我期待的123,而是这么一个奇怪的数字呢?
谢谢。
------解决方案--------------------
strValue.Format(_T("%f"),d); 试下改为
strValue.Format(_T("%f"),d.Lo64);
------解决方案--------------------
strValue.Format(_T("%lld"),d.Lo64);
这里d是一个结构体,另外lo64是long long类型,%f是对浮点型的。
------解决方案--------------------
先把DECIMAL 结构搞清楚
Represents a decimal data type that provides a sign and scale for a number (as in coordinates.)
Decimal variables are stored as 96-bit (12-byte) unsigned integers scaled by a variable power of 10. The power of 10 scaling factor specifies the number of digits to the right of the decimal point, and ranges from 0 to 28.
Syntax
JavaScriptC#C++F#JScriptVBView ColorizedCopy to Clipboard
typedef struct tagDEC {
USHORT wReserved;
union {
struct {
BYTE scale;
BYTE sign;
};
USHORT signscale;
};
ULONG Hi32;
union {
struct {
ULONG Lo32;
ULONG Mid32;
};
ULONGLONG Lo64;
};
} DECIMAL;
typedef struct tagDEC {
USHORT wReserved;
union {
struct {
BYTE scale;
BYTE sign;
};
USHORT signscale;
};
ULONG Hi32;
union {
struct {
ULONG Lo32;
ULONG Mid32;
};
ULONGLONG Lo64;
};
} DECIMAL;
Members
wReserved
Reserved.
scale
The number of decimal places for the number. Valid values are from 0 to 28. So 12.345 is represented as 12345 with a scale of 3.
sign
Indicates the sign; 0 for positive numbers or DECIMAL_NEG for negative numbers. So -1 is represented as 1 with the DECIMAL_NEG bit set.
signscale
The sign and number of decimal places.
Hi32
The high 32 bits of the number.
Lo32
The low 32 bits of the number.
Mid32
The middle 32 bits of the number.
Lo64
The low 64 bits of the number. This is an _int64.