nsdecimalnumber应该如何存储在数据库中? [英] how should nsdecimalnumber's be stored in the database?

查看:111
本文介绍了nsdecimalnumber应该如何存储在数据库中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在数据库中存储nsdecimalnumbers的最佳方法是什么?像float,real和double这样的数字类型是不合适的,因为保持货币使用的精确度很重要。

What is the best way to store nsdecimalnumbers in a database? Numeric types like float, real and double are out, because it is important to preserve the precision for currency usage.

我考虑的两个选择是字符串并存储两个整数一个拿着尾数,一个拿着指数。后一种方法面临的挑战是我没有看到一种简单的方法来将尾数和指数从nsdecimalnumber中拉出来。我可以从NSDecimalNumber获取NSDecimal,但尾数和指数属性似乎是私有的!也许我可以反转数字=(尾数* 10 ^指数),但是有更好的方法吗?

The two alternatives I have considered are string and storing two ints, one holding the mantissa and one holding the exponent. The challenge facing the latter approach is that I dont see an easy way to pull the mantissa and exponent out of the nsdecimalnumber. I can get NSDecimal from NSDecimalNumber, but the mantissa and exponent properties seem to be private! Perhaps I can reverse number = (mantissa * 10^exponent), but is there a better way to do this?

我的要求是我需要执行数据库中此字段的一些总和操作。如果我以字符串形式存储,那么执行此计算可能会更困难。我仍然需要编写一个自定义函数来对数据库中的尾数和指数求和,所以也许它仍然很重。

The requirement I have is that I would need to perform some aggregate sum operations on this field in the database. If I store as a string, perhaps performing this calculation would be more difficult. I would still need to write a custom function to sum over a mantissa and exponent in the database, so maybe its still as heavy.

编辑:我可以访问指数,但尾数保持一种奇怪的方式......在调试器中对它进行调查显示它是一个短数组(整数)。

I can access the exponent, but the mantissa is kept in a weird manner...poking at it in the debugger reveals that it is an array of short(ints).

推荐答案

NSDecimalNumber符合 NSCoding ,因此您可以使用 encodeWithCoder: 之前将其添加到您的数据库,然后使用 initWithCoder: 从数据库中检索时。

NSDecimalNumber conforms to NSCoding, so you could encode it with encodeWithCoder: before adding it to your database, and then decode it using initWithCoder: when retrieving it from the database.

请参阅存档和序列化编程指南,特别是编码和解码对象有关使用的更多详细信息编码器。

See the Archives and Serializations Programming Guide, specifically "Encoding and Decoding Objects" for more details on using encoders.

这篇关于nsdecimalnumber应该如何存储在数据库中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆