Re: NSDecimalSeparator localization II
Re: NSDecimalSeparator localization II
- Subject: Re: NSDecimalSeparator localization II
- From: Mark Dawson <email@hidden>
- Date: Thu, 30 Dec 2004 10:02:14 -0800
I wanted to develop a routine that stripped trailing zero's from a formatted (%f) string, including the decimal separator, if the number ends in "0" (such as 1.0--> 1). I think my code works, with the exception of the user setting the separator to the '1'--'9' set (I assume from Sean's comments, that any stripping code can NOT be perfect, given the user could set the separator to one of the stripping characters). Since the string comes from a %f format, the only characters that should be in the string are '0'--'9' and the decimal separator.
Can anyone spot flaws in the following stripping code (either logic or some newbie Cocoa mistake)?
- (void)stripTrailingZeros:(NSString **)fromString
{
NSString *aString = *fromString;
if (aString == nil)
return;
unsigned int len = [aString length];
if (len <= 2) // can't have trailing zero's with a string length of 2 or less
return;
// generally, decimal separator is one character in length, but user can set to be different
short decimalLen = [[[NSUserDefaults standardUserDefaults] objectForKey:NSDecimalSeparator] length];
Boolean found = false;
unichar c;
short i = len - 1;
while (!found && i >= 0)
{
c = [aString characterAtIndex:i];
if (c != '0') // done
found = true;
else
--i;
}
if (i != len - 1) // did some stripping, so need to update string
{
// if not another digit, then the decimal separator that stopped the parse
if (!(c >= '1' && c <= '9'))
i -= decimalLen;
aString = [*fromString substringToIndex:i+1];
*fromString = aString;
}
}
----------
From: Sean McBride <email@hidden>
Just tried something else: Sys Prefs also allows you to enter numbers as
the decimal separator! This can lead to some interesting parsing problems.
From: Clark Cox <email@hidden>
Reply-To: Clark Cox <email@hidden>
Date: Mon, 27 Dec 2004 07:23:33 -0500
To: Mark Dawson <email@hidden>
Cc: email@hidden
Subject: Re: NSDecimal localization
On Fri, 24 Dec 2004 00:10:54 -0800, Mark Dawson <email@hidden> wrote:
Is there ever a case where NSDecimal is NOT a unichar? Its defined as
a string, but the only two separators that I know of are the point and
the comma.
Why assume? Just treat it as a string, and automatically be insulated
against future changes, etc. Besides, I just tested it, and System
Preferences allows me to enter decomposed characters as the separator
characters, those will take more than one unichar to represent.
In general, when dealing with Unicode, one should never deal with
individual characters or codepoints, one should always deal with
strings, as there is no one-to-one mapping between what the user sees
as a single character, and what the programmer sees as a codepoint.
--
Clark S. Cox III
email@hidden
http://www.livejournal.com/users/clarkcox3/
http://homepage.mac.com/clarkcox3/
_______________________________________________
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