Re: Formating a number in a string
Re: Formating a number in a string
- Subject: Re: Formating a number in a string
- From: Amilcar Meneses Viveros <email@hidden>
- Date: Mon, 22 Mar 2004 08:36:11 -0600 (CST)
>
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?
Try with this
int sec = 123.44;
String *aString = [NSString stringWithFormat: @"my number is %4.2f", sec];
NSLog (aString);
the format of the decimal number is in this case "%4.2f"
You can see more about the format number in the book
- The C Programming Language, Second Edition
by Brian W. Kernighan and Dennis M. Ritchie.
Prentice Hall, Inc., 1988
regards
_______________________________________________
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.