如何使用 NSNumberFormatter 在 UITextField 中打印货币
我是这里的N00b。
我像这样打印我的货币:
-(IBAction)buttonPressed1:(id)sender
{
double currency = [Amount1.text doubleValue] + [Amount2.text doubleValue];
SumCurrency.text = [NSString stringWithFormat:@"%0.0f", answer];
}
我只是想使用 NSSNumberFormatter 以美国货币格式打印 SumCurrency.text ..遇到很多麻烦..请hekp
同样的问题??? http://groups.jonzu.com/z_apple_using-a-nsnumberformatter -with-a-uitextfield.html
提前致谢。
问候 , N00b
I am a N00b here .
I print my currency like this :
-(IBAction)buttonPressed1:(id)sender
{
double currency = [Amount1.text doubleValue] + [Amount2.text doubleValue];
SumCurrency.text = [NSString stringWithFormat:@"%0.0f", answer];
}
I just simply want to use NSSNumberFormatter to print the SumCurrency.text in US Currency format .. Having a lot of trouble with it ..Please hekp
same issue ??? http://groups.jonzu.com/z_apple_using-a-nsnumberformatter-with-a-uitextfield.html
Thanks in Advance .
Regards ,
N00b
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我得到了答案..但供任何人将来参考
,或者用 Swift 表示:
I got the answer .. But for anyone's future reference
or, in Swift:
这是 Objective-C 中的类别大放异彩的一个例子。
最干净的解决方案是为 NSNumber 创建一个类别。 .h:
和 .m:
在你的代码中,你必须 #import "NSNumber+Formatter.h" 并简单地执行以下操作:
非常清晰和干净。
This is an example of a situation where categories in Objective-C shine.
The cleanest solution is to create a category for NSNumber. .h:
and .m:
In your code, you'd have to #import "NSNumber+Formatter.h" and simply do this:
Very clear and clean.
使用 float 或 double(浮点算术)来处理货币是一个非常糟糕的主意,因为精度错误。您应该使用定点算术,又名 NSDecimalNumber。
Using float or double (floating point arithmetic) with money is a very bad idea because precision errors. You should use fixed-point arithmetic, aka NSDecimalNumber.