NSDeciamlNumber 3.33 插入数据库时变为 0.32999999999999996 sql是正确的,但是执行sql时数据变成了0.32999999999999996。 我正在使用 FMDB,我尝试了列类型 REAL、DECIMAL,它们有同样的问题。 如果有办法将比例设置为 DECIMAL 列。我试过 DECIMAL(8,2),但没有用。
最佳答案
据我所知,SQLite 不提供实际的十进制类型,所有类似 float 的数字都被转换为 double 。
http://www.sqlite.org/datatype3.html
对于小数 (10,5):
For conversions between TEXT and REAL storage classes, SQLite considers the conversion to be lossless and reversible if the first 15 significant decimal digits of the number are preserved. If the lossless conversion of TEXT to INTEGER or REAL is not possible then the value is stored using the TEXT storage class.
REAL(8 字节“double”)是 float 据的常用类型,如果要存储 3.33 的实际值,将会遇到转换问题。
您的选择:
- 存储一个字符串。速度慢、内存效率低、维护烦人,但保证正确。
- 如果您知道需要多少个小数点(例如,货币值),请存储一个 int。
- 存储一个 BLOB,然后以您喜欢的任何格式自行转换为二进制。
- 存一真(双),活之小异。如果您不存储货币,这通常没问题。
如果您想存储货币,不要使用 float 学,而是使用 int 来存储美分(或类似面额)的数量。
关于ios - 四舍五入问题将 NSDecimalNumber 插入到 Sqlite 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19861794/