objective-c - NSNumberFormatter 货币删除尾随零

标签 objective-c currency nsnumberformatter

我想将价格格式化为 45.50,但我不想要 45.00 这样的价格。我怎样才能避免这种情况?

最佳答案

我是这样做的:

NSNumber *amount = @(50.5);
NSNumberFormatter *currencyFormat = [[NSNumberFormatter alloc] init];
[currencyFormat setNumberStyle:NSNumberFormatterCurrencyStyle];
[currencyFormat setLocale:[NSLocale currentLocale]];

if (trunc(amount.floatValue) == amount.floatValue) {
    [currencyFormat setMaximumFractionDigits:0];
} else {
    [currencyFormat setMaximumFractionDigits:2];
}

NSLog(@"%@", [currencyFormat stringFromNumber:amount]);

我喜欢这个解决方案,因为它很简单。输出将为 $50.50。对于 amount = @(50.0) 将是 $50

关于objective-c - NSNumberFormatter 货币删除尾随零,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12262979/

相关文章:

objective-c - 如何制作这样的 UIBarButtonItem

ios - UIAlertView 中的多个 UITextField

PHP money 格式在需要时添加小数点和逗号

iphone NSNumberFormatter 在解析时在模拟器上工作,而不是在设备操作系统 4.2 上工作

iphone - UITableView 背景作为平铺图像

iphone - 特定行的 UITableViewCell 编辑样式

mysql - 加密货币 MySQL 数据类型 ?

sharepoint - 我应该使用 Sharepoint Number 列类型来存储货币值吗?

cocoa-touch - 如何在 iOS 上格式化和打印 float ?

ios - Swift IAP SK产品显示错误价格