How not to show unnecessary zeros when specifying integers, but swim if necessary

I have an application that I am developing, and one of my functions gives answers in float or double fields if necessary and an integer when the answer is an integer

so, for example, if the answer comes to 8.52, the answer becomes 8.52, but when the answer is 8, then the answer is 8 instead of 8.0000, I do not want it to show all the extra 0.

- (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 does not work

+6
source share
4 answers

These qualifiers are standard IEEE format specifications , which means that you can do things like %.2f to only show 2 decimal places on a float.

You can also convert it to int , and then use the %d format specifier if you want to do it this way.

There is also Apple documentation on this.

EDIT: Based on your comment on another post, it looks like you're looking for %g , which essentially removes extraneous 0 from the floats.

 display.text= [NSString stringWithFormat:@"%g",[val doubleValue]+[storage doubleValue]]; 

I found the answer here: Use printf to format floats without decimals, unless completion is 0s

+12
source

You can try this method call:

 [NSString stringWithFormat:@"%.0f", [val doubleValue] + [storage doubleValue]]; 
0
source

Formatting EDIT

This is how I did it when I needed to display the currency (but integers if the currency was a round number.

First we get the amount of money as a string

 NSString *earnString = _money.payout.displayableAmount; NSMutableString *strippedString = [NSMutableString stringWithCapacity:earnString.length]; 

// scan the string to remove anything other than numbers (including decimal 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 a β€œchange” to display this amount if (earnInt <100) {// The dollar amount is less than the dollar indication only in 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 an integer amount in dollars, display it correctly} 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 return them back along with the decimal space.

 }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; } 

Hope this helps you ...

0
source

The easiest way is NSNumberFormatter. If necessary, only a decimal number will be displayed. 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.

0
source

Source: https://habr.com/ru/post/956975/


All Articles