gpt4 book ai didi

ios - 使用 NSNumberFormatter 防止小数

转载 作者:IT王子 更新时间:2023-10-29 08:13:15 27 4
gpt4 key购买 nike

我有一个 NSNumberFormatter,我试图用它从一个 NSNumber 生成一个整数的 GBP (£)。无论我尝试哪种咒语,我都会得到两位小数。我的代码是:

NSNumberFormatter *fmtCurrency = [[[NSNumberFormatter alloc] init] autorelease];
[fmtCurrency setNumberStyle: NSNumberFormatterCurrencyStyle];
[fmtCurrency setGeneratesDecimalNumbers:FALSE];
[fmtCurrency setCurrencyCode:@"GBP"];
[fmtCurrency setCurrencySymbol:@"£"];
txtTotal.text = [fmtCurrency stringFromNumber: result.Bill ]; // this is an NSNumber

最佳答案

我想你想用零调用 setMaximumFractionDigits。

NSNumberFormatter *fmtCurrency = [[[NSNumberFormatter alloc] init] autorelease];
[fmtCurrency setNumberStyle: NSNumberFormatterCurrencyStyle];
[fmtCurrency setGeneratesDecimalNumbers:NO];
[fmtCurrency setCurrencyCode:@"GBP"];
[fmtCurrency setCurrencySymbol:@"£"];

NSNumber* myNumber = [NSNumber numberWithFloat:12.34];
NSString* myString = [fmtCurrency stringFromNumber:myNumber];
NSLog( @"the number is %@", myString );

[fmtCurrency setMaximumFractionDigits:0];
myString = [fmtCurrency stringFromNumber:myNumber];
NSLog( @"now it's %@", myString );


2011-07-25 01:05:39.823 FormatterTest[841:707] the number is £12.34
2011-07-25 01:05:39.824 FormatterTest[841:707] now it's £12

关于ios - 使用 NSNumberFormatter 防止小数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6813095/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com