Formatting number in Objective-C


I have an app that does a calculation and returns a float , type 4/2 = 2.00 but I would like this calculation to show only 2 , and if the result was with decimal places with numbers other than 0 it would show the decimals.

How to show a float result that ignores unnecessary zeros?


Using %g to set the precision, do something like this:

float num1 = 2.00;
float num2 = 2.34;

NSLog(@"%g, %g", num1, num2);


2, 2.34

Scroll to Top