iOS-使用NSNumberFormatter显示不含美分的货币
我能够使用NSNUmberFormatter将特定NSNumber转换为货币。其showign$符号、以“,”等分隔的数字iOS-使用NSNumberFormatter显示不含美分的货币,ios,nsnumberformatter,Ios,Nsnumberformatter,我能够使用NSNUmberFormatter将特定NSNumber转换为货币。其showign$符号、以“,”等分隔的数字 但我不想展示分钱 例如,它显示$537335.32 我只想展示$536335 我正在编写以下代码: NSNumberFormatter *currencyFormatter = [[[NSNumberFormatter alloc] init] autorelease]; [currencyFormatter setNumberStyle:NSNumberFormatter
但我不想展示分钱
例如,它显示$537335.32
我只想展示$536335
我正在编写以下代码:
NSNumberFormatter *currencyFormatter = [[[NSNumberFormatter alloc] init] autorelease];
[currencyFormatter setNumberStyle:NSNumberFormatterCurrencyStyle];
NSDictionary* tempDict = [self.displayDict objectForKey:kConservative];
conservativeHP.text = [currencyFormatter stringFromNumber:[tempDict objectForKey:kHomePrice]];
如何创建NSNumberFormatter或执行某些操作,使结果不会有“小数”在将数字添加到格式化程序之前,请将其更改为int以避免小数,如下所示:
float myValue = 543.32;
NSNumber myNumber = [NSNumber numberWithInt: (int)myValue)];
你将去掉小数部分 使用maximumFractionDigits设置:
NSNumberFormatter *currencyFormatter = [[[NSNumberFormatter alloc] init] autorelease];
[currencyFormatter setNumberStyle:NSNumberFormatterCurrencyStyle];
[currencyFormatter setMaximumFractionDigits:2];
您可以相应地调整该值。使用2会将结果限制为两位数,对于这个问题,值为0就可以了。当您要显示它时,只需执行此操作
Mylabel.text = @"%.2f", mynumberwithcents;
这仅在mynumberwhithcents
变量为浮点或双精度时有效。Swift 3:
currencyFormatter.maximumFractionDigits = 0
如果使用类别:
H
#进口
@接口字符串(价格)
+(NSString*)updateStringToPriceWithValue:(NSNumber*)值;
@结束
M
#导入“NSString+Price.h”
@实现NSString(价格)
+(NSString*)updateStringToPriceWithValue:(NSNumber*)值
{
NSNumberFormatter*格式化程序=[[NSNumberFormatter alloc]init];
[格式化程序setNumberStyle:NSNumberFormatterCurrencyStyle];
NSInteger minimumFractionDigits=[格式化程序minimumFractionDigits];
NSInteger maximumFractionDigits=[格式化程序maximumFractionDigits];
NSString*格式化=[formatter stringFromNumber:值];
NSLog(@“%@”,格式化);
[格式化程序设置minimumFractionDigits:minimumFractionDigits];
[格式化程序设置maximumFractionDigits:maximumFractionDigits];
返回格式;
}
这是swift 3中的解决方案
let formatter = NumberFormatter()
formatter.numberStyle = .currency
//formatter.locale = NSLocale.current
formatter.currencySymbol = "" //In case you do not want any currency symbols
formatter.maximumFractionDigits = 0
将maximumFractionDigits
设置为0
将不会显示任何十进制或小数值
extension NumberFormatter {
static func makeDisplayPrice(byCode code: String, price: Int) -> String {
let currencyFormatter = NumberFormatter.currencyPrice
currencyFormatter.currencyCode = code
guard let priceString = currencyFormatter.string(from: NSNumber(integerLiteral: price)) else { return "" }
return priceString
}
static var currencyPrice: NumberFormatter {
let currencyFormatter = NumberFormatter()
currencyFormatter.usesGroupingSeparator = false
currencyFormatter.numberStyle = .currency
currencyFormatter.locale = Locale.current
currencyFormatter.maximumFractionDigits = 0
return currencyFormatter
}
}
谢谢,如果我正在创建NSNumber,这是一个很好的解决方案。但问题是,我将从服务器获取无法更改的数据。仅在显示过程中,我要截断美分。您不需要更改原始值,只需将数字复制到int变量中,我看不出问题。如果要正确执行,请使用[numberFormatter setRoundingMode:NSNumberFormatterRoundDown];除此之外,添加[currencyFormatter setMinimumFractionDigits:0];当小数等于零时,将得到四舍五入的数字。就我而言,我想拿出12.6万美元,即125万美元。这让我得到了。
extension NumberFormatter {
static func makeDisplayPrice(byCode code: String, price: Int) -> String {
let currencyFormatter = NumberFormatter.currencyPrice
currencyFormatter.currencyCode = code
guard let priceString = currencyFormatter.string(from: NSNumber(integerLiteral: price)) else { return "" }
return priceString
}
static var currencyPrice: NumberFormatter {
let currencyFormatter = NumberFormatter()
currencyFormatter.usesGroupingSeparator = false
currencyFormatter.numberStyle = .currency
currencyFormatter.locale = Locale.current
currencyFormatter.maximumFractionDigits = 0
return currencyFormatter
}
let displayPrice = NumberFormatter.makeDisplayPrice(byCode: currency, price: price)