如何在Objective-C/iOS中代表钱?

19 currency objective-c ios

我正在开发一款iPhone应用程序并且希望代表金额($)金额.我不能使用float因为它们会引入一定量的舍入错误.我可以用什么?

我正在考虑定义我自己的Money类,并在内部存储美元和便士作为NSInteger.

@interface Money : NSObject {
    //$10.25 is stored as dollas=10 and pennies=25
    NSInteger dollars;
    NSInteger pennies;
}
Run Code Online (Sandbox Code Playgroud)

另一种可能的表示(更容易添加和乘法)将使用单个NSInteger作为便士.

@interface Money : NSObject {
    //$10.25 is stored as pennies=1025
    NSInteger pennies;
}
Run Code Online (Sandbox Code Playgroud)

你的想法是什么?我可以使用"BigDecimal"类型吗?