編輯格式化
這是一種方式,我這樣做,當我需要顯示貨幣(但是如果貨幣是整數,則爲整數)。
首先我們得到的金額作爲字符串
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