我正在开发一个 iPhone 应用程序,并希望代表货币 ($) 金额。我不能使用浮点数,因为它们会引入一定数量的舍入误差。我可以使用什么?
我正在考虑在内部定义我自己的 Money 类并将美元和便士存储为 NSInteger。
@interface Money : NSObject {
//$10.25 is stored as dollas=10 and pennies=25
NSInteger dollars;
NSInteger pennies;
}
另一种可能的表示(更容易加法和乘法)是使用单个 NSInteger 作为便士。
@interface Money : NSObject {
//$10.25 is stored as pennies=1025
NSInteger pennies;
}
你觉得呢?你有没有什么想法?我可以使用“BigDecimal”类型吗?