Reputation: 89
I have an app I'm developing and one of my features is giving answers in float or double values when needed and an integer when the answer is a whole number
so for example if the answer comes out to 8.52 the answer becomes 8.52 but when the answer is 8 the answer is 8 instead of 8.0000, i don't want it to show all the extra 0s.
- (IBAction) equalsbutton {
NSString *val = display.text;
switch(operation) {
case Plus :
display.text= [NSString stringWithFormat:@"%qi",[val longLongValue]+[storage longLongValue]];
case Plus2 :
display.text= [NSString stringWithFormat:@"%f",[val doubleValue]+[storage doubleValue]];
this code doesn't seem to work
Upvotes: 7
Views: 3509
Reputation: 3560
Easiest way, is NSNumberFormatter. It will only display the decimal if needed. Example (Swift):
let num1: Double = 5
let num2: Double = 5.52
let numberFormatter = NSNumberFormatter()
numberFormatter.numberStyle = .DecimalStyle
print(numberFormatter.stringFromNumber(NSNumber(double: num1)))
print(numberFormatter.stringFromNumber(NSNumber(double: num2)))
This will print 5 and then 5.52.
Upvotes: 0
Reputation: 4703
You can try this method call:
[NSString stringWithFormat:@"%.0f", [val doubleValue] + [storage doubleValue]];
Upvotes: 0
Reputation: 1647
EDIT Formatting
Here is a way that I did this when I needed to display currency (but whole numbers if the currency was a round number.
First we get the money amount as a string
NSString *earnString = _money.payout.displayableAmount;
NSMutableString *strippedString = [NSMutableString
stringWithCapacity:earnString.length];
//scan the string to remove anything but the numbers (including decimals points)
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];
//if the string is less than 100 then we only had "change" so display that amount if(earnInt < 100){ //Dollar amount is less then dollar display just the cents and the cent symbol NSString *centString = [NSString stringWithFormat:@"%i¢", earnInt]; earnAmount.text = centString;
//if we have a number evenly divisible by 100 then we have whole dollar amount, display that properly }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;
//finally if we have a mixed number then put them back together with the decimal in-between.
}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;
}
Hopefully this helps you out...
Upvotes: 0
Reputation: 6597
These specifiers are standard IEEE format specifiers, which means that you can do things like %.2f
to only show 2 decimal places on a float variable.
You could also convert it into an int
, and then use the %d
format specifier if you wanted to do it that way.
Here's also Apple's documentation on the subject.
EDIT: Based on your comment on the other post, it looks like you're looking for %g
, which will essentially remove the extraneous 0's from floats.
display.text= [NSString stringWithFormat:@"%g",[val doubleValue]+[storage doubleValue]];
I found the answer here: Use printf to format floats without decimal places if only trailing 0s
Upvotes: 13