• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: Formating a number in a string
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

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.


  • Prev by Date: Re: Writing a non-NSDocument based multiple document window apps
  • Next by Date: Re: NSKeyedUnarchiver replaces NSStrings
  • Previous by thread: Re: Formating a number in a string
  • Next by thread: pdf data to cocoa objects
  • Index(es):
    • Date
    • Thread