编辑格式化
这是一种方式,我这样做,当我需要显示货币(但是如果货币是整数,则为整数)。
首先我们得到的金额作为字符串
NSString *earnString = _money.payout.displayableAmount;
NSMutableString *strippedString = [NSMutableString
stringWithCapacity:earnString.length];
//扫描串删除任何东西,但数字(包括小数点)
NSScanner *scanner = [NSScanner scannerWithString:earnString];
NSCharacterSet *numbers = [NSCharacterSet
characterSetWithCharactersInString:@""];
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则我们只有“更改”,所以显示金额 如果(赚到的钱< 100)//美元数额少于美元只显示美分和美分符号 NSString * centString = [NSString stringWithFormat:@“%i¢” ,earnInt]; earnAmount.text = centString;
//如果我们有一个数由100整除则有全金额,显示的是正确 }否则如果(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;
}
希望这可以帮助你......
谢谢你这个工作,我想要的方式! – user2933653