Re: NSDecimalSeparator localization II
Re: NSDecimalSeparator localization II
- Subject: Re: NSDecimalSeparator localization II
- From: Ricky Sharp <email@hidden>
- Date: Thu, 30 Dec 2004 18:57:36 -0600
On Dec 30, 2004, at 5:32 PM, Ricky Sharp wrote:
On Dec 30, 2004, at 4:07 PM, mark wrote:
So if I understand correctly (it works in my testing, but I wanted to
make
sure), I should change things to:
if ([[NSCharacterSet decimalDigitCharacterSet] characterIsMember:
'0'])
// if not '0' based, don't trim
{
aString = [*fromString
stringByTrimmingCharactersInSet:[NSCharacterSet
characterSetWithCharactersInString:@"0"]]; // strip trailing zero's
// Note: can't strip both 0's and decimal point at the same time
because it
// will overstrip--turing 10000.00 into 1, while we want it 10000
aString = [aString stringByTrimmingCharactersInSet:[NSCharacterSet
characterSetWithCharactersInString:(NSString *)[[NSUserDefaults
standardUserDefaults] objectForKey:NSDecimalSeparator]]];
// now strip trailing decimal point
*fromString = aString;
}
Be careful when using stringByTrimmingCharactersInSet as it will trim
from _both_ ends of the receiver.
The string 0.0050 will become 005
After thinking about this a bit more, you could still use
stringByTrimmingCharactersInSet, but first modify your string such that
it won't trim from the left. For example, prefix it with say 'A'.
After your trimming, remove that 'A'.
- (NSString*)stringWithFormattedFloat:(float)aValue
{
NSString* theString = [NSString stringWithFormat:@"%0.4lf", aValue];
theString = [NSString stringWithFormat:@"A%@", theString];
theString = [theString stringByTrimmingCharactersInSet:
[NSCharacterSet characterSetWithCharactersInString:@"0."]];
theString = [theString stringByTrimmingCharactersInSet:
[NSCharacterSet characterSetWithCharactersInString:@"A"]];
return theString;
}
This appears to work. Note that "0" will become the empty string, but
you mentioned that you already handle that special case. And of course
replace the hard-coded values with proper decimal separator.
___________________________________________________________
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