• 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: 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.


  • Follow-Ups:
    • Re: Formating a number in a string
      • From: Joe Lertola <email@hidden>
References: 
 >Formating a number in a string (From: Joe Lertola <email@hidden>)

  • Prev by Date: Re: Clicking Dock Icon creates a new window
  • Next by Date: Re: Formating a number in a string
  • Previous by thread: Formating a number in a string
  • Next by thread: Re: Formating a number in a string
  • Index(es):
    • Date
    • Thread