I made a simple calculator and Everytime I hit calculate it’ll give a an answer but gives six unnecessary zeros, my question, how can I remove those zeros?
NSString *firstString = textfieldone.text;
NSString *secondString = textfieldtwo.text;
NSString *LEGAL = @"0123456789";
NSCharacterSet *characterSet = [[NSCharacterSet characterSetWithCharactersInString:LEGAL] invertedSet];
NSString *filteredOne = [[firstString componentsSeparatedByCharactersInSet:characterSet]
componentsJoinedByString:@""];
NSString *filteredTwo = [[secondString componentsSeparatedByCharactersInSet:characterSet]
componentsJoinedByString:@""];
firstString = filteredOne;
secondString = filteredTwo;
//Here we are creating three doubles
double num1;
double num2;
double output;
//Here we are assigning the values
num1 = [firstString doubleValue];
num2 = [secondString doubleValue];
output = num1 + num2;
label.text = [NSString stringWithFormat:@"%f",output];
Example:
15 + 15 = 30.000000
If you’re displaying this by using a string, check the following approaches.
NSString
Note: You can specify the number of decimals you want to return by adding a point and a number before the ‘f’
-Edited-
In your case use the following approach: