NSNumberFormatter dropping trailing zeros?
NSNumberFormatter dropping trailing zeros?
- Subject: NSNumberFormatter dropping trailing zeros?
- From: Ryan Stephens <email@hidden>
- Date: Mon, 8 Mar 2010 13:06:34 -0800
Hi all,
I'm trying to use an NSNumberFormatter to format the text of a
UITextField as a user enters characters. The issue I'm currently
having is that the number formatter always drops a trailing zero. For
example, see the following code:
- (BOOL)textField:(UITextField *)textField
shouldChangeCharactersInRange:(NSRange)range
replacementString:(NSString *)string
{
NSNumberFormatter *formatter = [[[NSNumberFormatter alloc] init]
autorelease];
[formatter setFormatterBehavior:NSNumberFormatterBehavior10_4];
[formatter setNumberStyle:NSNumberFormatterCurrencyStyle];
[formatter setLocale:[NSLocale currentLocale]];
NSString *currentValue = [[formatter
numberFromString:textField.text] stringValue];
NSLog(@"current value: %@", currentValue);
}
Assuming currentLocal == en_US and textField.text == "$2.30",
[formatter numberFromString:textField.text] returns 2.3. I'd like it
to return 2.30, so that after removing the decimal point from
currentValue, appending string (assuming string = "0") and inserting
the decimal point again, currentValue would be "23.00" and [formatter
stringFromNumber:[NSDecimalNumber
decimalNumberWithString:currentValue]] would return $23.00.
I've tried adjusting a few properties of the NSNumberFormatter
(setMinimumFractionsDigits: 2 and setMinimumSignificantDigits: 3 to be
specific), but that didn't seem to make any impact similar to the
change I'm looking for. My only other thought would be to try and
adjust roundingIncrement, but I'm not sure what value I'd be looking
to use. The NSNumberFormatter docs, Data Formatting Guide and Google
don't seem to be providing any other hints. Any advice?
Thanks,
Ryan
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden