Re: decimal floating formatting question
Re: decimal floating formatting question
- Subject: Re: decimal floating formatting question
- From: Ricky Sharp <email@hidden>
- Date: Wed, 22 Dec 2004 18:34:52 -0600
On Dec 22, 2004, at 5:59 PM, Mark Dawson wrote:
Is there a way to format a decimal number with no trailing zeros and
show only "X" number of digits? I would like 1.0 to show up as "1",
1.2 as '1.2";, 1.31457 as "1.3146".
I'm familiar with the "%.4f" type format, but while this limits the
output to 4 decimal points, it adds trailing zeros (i.e., "1.2000").
Is there a way to the best of both worlds via a formatting option?
Don't think there's a way to do it with a printf-style formatter.
Looks like you'll just have to post-process the string.
At first glance, stringByTrimmingCharactersInSet: seems a good
candidate, but it unfortunately trims both ends of the string.
While a mixture of calls to NSString APIs could ultimately do what you
want and handle all edge cases, it may be easier to just write a loop
and manually eat right-padded zeros (and then the decimal point if no
digits remain).
___________________________________________________________
Ricky A. Sharp mailto:email@hidden
Instant Interactive(tm) http://www.instantinteractive.com
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden