Re: Formating a number in a string
Re: Formating a number in a string
- Subject: Re: Formating a number in a string
- From: Chris Ridd <email@hidden>
- Date: Sun, 14 Mar 2004 14:56:12 +0000
On 14/3/04 2:23 pm, Joe Lertola <email@hidden> wrote:
>
Hi all,
>
>
I am new to cocoa and C programming in general, so I hope you will
>
excuse me if I ask an obvious question. I have been puttering around
>
the help system for over an hour without finding the answer to this.
>
>
I want to add a floating point number to string as in this example
>
below:
>
>
float sec;
>
NSString *degreeString;
>
>
sec = 123.44;
>
degreeString = [NSString stringWithFormat: @"my number is %f", sec];
>
>
When I run this I get a string like this:
>
"my number is 123.440002"
>
>
How can I limit the number that is inserted into the string to two
>
decimal places?
Replace the "%f" with "%.2f". This notation is from the standard C printf
family of functions - if you type 'man printf' at a terminal (or use Xcode)
then you should see the other things you can do.
Cheers,
Chris
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.