编辑格式
这是我在需要显示货币时执行此操作的一种方式(如果货币是整数,则为整数。
首先,我们将金额作为字符串
NSString *earnString = _money.payout.displayableAmount;
NSMutableString *strippedString = [NSMutableString
stringWithCapacity:earnString.length];
//扫描字符串以删除除数字以外的任何内容(包括小数点)
NSScanner *scanner = [NSScanner scannerWithString:earnString];
NSCharacterSet *numbers = [NSCharacterSet
characterSetWithCharactersInString:@"0123456789"];
while ([scanner isAtEnd] == NO) {
NSString *buffer;
if ([scanner scanCharactersFromSet:numbers intoString:&buffer]) {
[strippedString appendString:buffer];
} else {
[scanner setScanLocation:([scanner scanLocation] + 1)];
}
}
//create an int with this new string
int earnInt = [strippedString intValue];
//如果字符串小于 100,那么我们只有“更改”,所以显示金额 if(earnInt < 100){ //美元金额小于美元,只显示美分和美分符号 NSString *centString = [NSString stringWithFormat :@"%i¢", EarnInt]; EarnAmount.text = centString;
//如果我们有一个能被 100 整除的数字,那么我们有整个美元金额,正确显示 }else if(earnInt % 100 == 0){
//The amount is exactly a dollar, display the whole number
NSString *wholeDollar = [NSString stringWithFormat:@"$%i", (earnInt/100)];
earnAmount.text = wholeDollar;
//最后,如果我们有一个混合数字,那么将它们与中间的小数放在一起。
}else{
//Dollar amount is not exactly a dollar display the entire amount
NSString *dollarString = [NSString stringWithFormat: @"$%0d.%02d", (earnInt / 100), (earnInt % 100)];
earnAmount.text = dollarString;
}
希望这可以帮助你...