Re: Formatter - 2 decimal places
Re: Formatter - 2 decimal places
- Subject: Re: Formatter - 2 decimal places
- From: Ralph Pöllath <email@hidden>
- Date: Mon, 17 Nov 2003 11:54:29 +0100
Hi,
On 17.11.2003, at 06:49, Jay Rimalrick wrote:
Is there a way to limit a users input to two decimal places? By using
the interface builder formatters the user can type in 9.9999 and when
they click off of the text box it rounds up to 10 but I want then to
only be able to enter 9.99 then it stops accepting their input.
I think you can do what you want by subclassing NSFormatter, see an
example below. Then you can either make a IB palette for your formatter
or assign it to your text fields programatically. This is probably not
a perfect example, since it works on strings and doesn't make sure
those strings are actually numbers at all. In your case, you might
consider subclassing NSNumberFormatter.
HTH,
-Ralph.
---
// MyFormatter.h
#import <Foundation/Foundation.h>
@interface MyFormatter : NSFormatter
{
}
@end
// MyFormatter.m
#Import "MyFormatter.h"
@implementation MyFormatter
- (BOOL)getObjectValue:(id *)obj forString:(NSString *)string
errorDescription:(NSString **)error
{
*obj = string;
return YES;
}
- (NSString *)stringForObjectValue:(id)anObject
{
if ([anObject isKindOfClass: [NSString class]])
return anObject;
return nil;
}
- (BOOL)isPartialStringValid:(NSString *)partialString
newEditingString:(NSString **)newString errorDescription:(NSString
**)error
{
// don't accept strings with more than 2 decimals after the last dot
// you'll have to implement numberOfDecimals yourself :-)
if ([partialString numberOfDecimals] > 2)
{
NSBeep();
return NO;
}
*newString = partialString;
return YES;
}
@end
_______________________________________________
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.