30 January 2009

Author

Jan Vantomme

Categories

iPhone
Workflow

Formatting numbers

This one is just a quick tip on presenting decimal numbers on a label or logging them to the console. In my last article on using NSLog, I wrote about using format specifiers like %f, %i and %@ to log variables to the console.

If you use %f to display or log a float, you will always see the full number. If you use the format specifier %.2f, the number will be shortened with only two decimal digits.

float myNumber = 134.65987239;
NSLog(@"This is a decimal number: %f", myNumber);
NSLog(@"This is a decimal number: %.2f", myNumber);
myLabel.text =
    [NSString stringWithFormat:@"%.2f", myNumber];

This code example will print these two lines to the console:
This is a decimal number: 134.65987239
This is a decimal number: 134.66
The last line of the code example will also set the text of myLabel to 134.66

Top · Tweet about this

Browse Articles

Previous Article:
Next Article:

0 Opinions posted so far. Now go post your own. To the comment form!

Commenting is closed for this article.

Subscribe to this blog

About this blog

This is the personal weblog of Jan Vantomme.
I write about the everyday things that move me as a designer. I write shorter things on Twitter.

Add to Technorati Favorites

Some of the blogs I like

John Nack on Adobe
Blog about Adobe Photoshop & Photography by John Nack
Shaun Inman
A blog on web design and development by Shaun Inman
Aral Balkan
Confessions of an internet junkie.
Jeffrey Zeldman
Webdesign news and info by Jeffrey Zeldman
Authentic Boredom
A blog on web design by Cameron Moll