Reputation: 3725
I want to format prices like 45.50 but I don't want prices like 45.00. How can I avoid this?
Upvotes: 3
Views: 2535
Reputation: 1425
I did it this way:
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]);
I like this solution for its simplicity. Output will be $50.50
. And for amount = @(50.0)
will be $50
Upvotes: 4
Reputation: 38475
If you're just after a very quick and dirty hack . . .
// Get the price as a string
NSString *priceString = [NSString stringWithFormat:@"%2.2f", priceFloat];
// Trim if needed
if ([priceString hasSuffix:@".00"])
priceString = [priceString substringToIndex:priceString.length-3];
NB This method won't work for localised content i.e. In Europe the decimal separator is a comma so you will see 45,00, not 45.00.
Upvotes: 1
Reputation: 38239
Do this:
NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];
[formatter setNumberStyle:NSNumberFormatterDecimalStyle];
[formatter setMaximumFractionDigits:2];
[formatter setRoundingMode: NSNumberFormatterRoundUp];
NSString *numberString = [formatter stringFromNumber:[NSNumber numberWithFloat:22.368511]];
NSLog(@"Result...%@",numberString);//Result 22.37
Now trail unwanted like this:
NSString* CWDoubleToStringWithMax2Decimals(double d) {
NSString* s = [NSString stringWithFormat:@"%.2f", d];
NSCharacterSet* cs = [NSCharacterSet characterSetWithCharacterInString:@"0."];
NSRange r = [s rangeOfCharacterInSet:cs
options:NSBackwardsSearch | NSAnchoredSearch];
if (r.location != NSNotFound) {
s = [s substringToIndex:r.location];
}
return s;
}
Upvotes: 1
Reputation: 1920
INPUT : 12.74 OR 12.745
NSString *inputString=[NSString string];
inputString=[NSString stringWithFormat:@"%.4g",12.74f];
NSLog(@"inputString : %@ \n\n",inputString);
OUTPUT:
inputString : 12.74
INPUT : 12.00 OR 12.000
NSString *inputString=[NSString string];
inputString=[NSString stringWithFormat:@"%.4g",12.00f];
NSLog(@"inputString : %@ \n\n",inputString);
OUTPUT:
inputString : 12 UPDATED ANSWER: for his comment question
INPUT:12.30
i assume here he is going to show this value in some UI like UILabel,.....Not For Calculation.
NSString *inputString=[NSString string];
inputString=[NSString stringWithFormat:@"%.4g",12.30f];
NSArray *arr=[inputString componentsSeparatedByString:@"."];
if ([arr count] >= 2) {
NSString *secondStr=[arr objectAtIndex:1];
if ([secondStr length]<2) {
inputString=[NSString stringWithFormat:@"%@0",inputString];
}
}
NSLog(@"inputString : %@ \n\n",inputString); OUTPUT:
inputString : 12.30
Upvotes: 0
Reputation: 8267
float myOriginalPrice = 45.50;
CGFloat mod = fmod(myOriginalPrice, 1);
if (mod == 0){
mod = (int)myOriginalPrice;
NSLog(@"%.0f", mod);
} else {
NSLog(@"%f", myOriginalPrice);
}
Upvotes: 0