2005-02-22 11:22:44 +00:00
|
|
|
/**
|
2000-09-02 01:44:24 +00:00
|
|
|
NSNumberFormatter class
|
|
|
|
Copyright (C) 2000 Free Software Foundation, Inc.
|
|
|
|
|
|
|
|
Written by: Fred Kiefer <FredKiefer@gmx.de>
|
|
|
|
Created: July 2000
|
2001-10-19 09:10:04 +00:00
|
|
|
Updated by: Richard Frith-Macdonald <rfm@gnu.org> Sept 2001
|
2000-09-02 01:44:24 +00:00
|
|
|
|
|
|
|
This file is part of the GNUstep Base Library.
|
|
|
|
|
|
|
|
This library is free software; you can redistribute it and/or
|
|
|
|
modify it under the terms of the GNU Library General Public
|
|
|
|
License as published by the Free Software Foundation; either
|
|
|
|
version 2 of the License, or (at your option) any later version.
|
|
|
|
|
|
|
|
This library is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
Library General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU Library General Public
|
|
|
|
License along with this library; if not, write to the Free
|
2006-10-21 09:49:18 +00:00
|
|
|
Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
|
|
|
|
Boston, MA 02111 USA.
|
2001-12-18 16:54:15 +00:00
|
|
|
|
|
|
|
<title>NSNumberFormatter class reference</title>
|
|
|
|
$Date$ $Revision$
|
2000-09-02 01:44:24 +00:00
|
|
|
*/
|
|
|
|
|
2003-06-07 01:24:41 +00:00
|
|
|
#include "Foundation/NSAttributedString.h"
|
|
|
|
#include "Foundation/NSDecimalNumber.h"
|
2005-10-17 10:47:54 +00:00
|
|
|
#include "Foundation/NSDictionary.h"
|
|
|
|
#include "Foundation/NSException.h"
|
2003-06-07 01:24:41 +00:00
|
|
|
#include "Foundation/NSNumberFormatter.h"
|
2005-10-17 10:47:54 +00:00
|
|
|
#include "Foundation/NSString.h"
|
|
|
|
#include "Foundation/NSUserDefaults.h"
|
2000-09-02 01:44:24 +00:00
|
|
|
|
|
|
|
|
|
|
|
@implementation NSNumberFormatter
|
|
|
|
|
2001-10-19 09:10:04 +00:00
|
|
|
- (BOOL) allowsFloats
|
2000-09-02 01:44:24 +00:00
|
|
|
{
|
2001-10-19 09:10:04 +00:00
|
|
|
return _allowsFloats;
|
2000-09-02 01:44:24 +00:00
|
|
|
}
|
|
|
|
|
2001-10-19 09:10:04 +00:00
|
|
|
- (NSAttributedString*) attributedStringForObjectValue: (id)anObject
|
|
|
|
withDefaultAttributes: (NSDictionary*)attr
|
2000-09-02 01:44:24 +00:00
|
|
|
{
|
2005-10-17 10:47:54 +00:00
|
|
|
float val;
|
|
|
|
|
|
|
|
if (anObject == nil)
|
|
|
|
{
|
|
|
|
return [self attributedStringForNil];
|
|
|
|
}
|
|
|
|
else if (![anObject isKindOfClass: [NSNumber class]])
|
|
|
|
{
|
|
|
|
return [self attributedStringForNotANumber];
|
|
|
|
}
|
|
|
|
else if ([anObject intValue] == 0)
|
|
|
|
{
|
|
|
|
return [self attributedStringForZero];
|
|
|
|
}
|
|
|
|
|
|
|
|
val = [anObject intValue];
|
|
|
|
if ((val > 0) && (_attributesForPositiveValues))
|
|
|
|
{
|
|
|
|
attr = _attributesForPositiveValues;
|
|
|
|
}
|
|
|
|
else if ((val < 0) && (_attributesForNegativeValues))
|
|
|
|
{
|
|
|
|
attr = _attributesForNegativeValues;
|
|
|
|
}
|
|
|
|
|
2005-02-22 11:22:44 +00:00
|
|
|
return AUTORELEASE([[NSAttributedString alloc] initWithString:
|
2005-10-17 10:47:54 +00:00
|
|
|
[self stringForObjectValue: anObject] attributes: attr]);
|
2001-10-19 09:10:04 +00:00
|
|
|
}
|
2000-09-02 01:44:24 +00:00
|
|
|
|
2001-10-19 09:10:04 +00:00
|
|
|
- (NSAttributedString*) attributedStringForNil
|
|
|
|
{
|
|
|
|
return _attributedStringForNil;
|
2000-09-02 01:44:24 +00:00
|
|
|
}
|
|
|
|
|
2001-10-19 09:10:04 +00:00
|
|
|
- (NSAttributedString*) attributedStringForNotANumber
|
2000-09-02 01:44:24 +00:00
|
|
|
{
|
2001-10-19 09:10:04 +00:00
|
|
|
return _attributedStringForNotANumber;
|
2000-09-02 01:44:24 +00:00
|
|
|
}
|
|
|
|
|
2001-10-19 09:10:04 +00:00
|
|
|
- (NSAttributedString*) attributedStringForZero
|
2000-09-02 01:44:24 +00:00
|
|
|
{
|
2001-10-19 09:10:04 +00:00
|
|
|
return _attributedStringForZero;
|
2000-09-02 01:44:24 +00:00
|
|
|
}
|
|
|
|
|
2001-10-19 09:10:04 +00:00
|
|
|
- (id) copyWithZone: (NSZone *)zone
|
2000-09-02 01:44:24 +00:00
|
|
|
{
|
2001-10-19 09:10:04 +00:00
|
|
|
NSNumberFormatter *c = (NSNumberFormatter*) NSCopyObject(self, 0, zone);
|
|
|
|
|
|
|
|
RETAIN(c->_negativeFormat);
|
|
|
|
RETAIN(c->_positiveFormat);
|
|
|
|
RETAIN(c->_attributesForPositiveValues);
|
|
|
|
RETAIN(c->_attributesForNegativeValues);
|
|
|
|
RETAIN(c->_maximum);
|
|
|
|
RETAIN(c->_minimum);
|
|
|
|
RETAIN(c->_roundingBehavior);
|
|
|
|
RETAIN(c->_roundingBehavior);
|
|
|
|
RETAIN(c->_attributedStringForNil);
|
|
|
|
RETAIN(c->_attributedStringForNotANumber);
|
|
|
|
RETAIN(c->_attributedStringForZero);
|
|
|
|
|
|
|
|
return c;
|
2000-09-02 01:44:24 +00:00
|
|
|
}
|
|
|
|
|
2001-10-19 09:10:04 +00:00
|
|
|
- (void) dealloc
|
2000-09-02 01:44:24 +00:00
|
|
|
{
|
2001-10-19 09:10:04 +00:00
|
|
|
RELEASE(_negativeFormat);
|
|
|
|
RELEASE(_positiveFormat);
|
|
|
|
RELEASE(_attributesForPositiveValues);
|
|
|
|
RELEASE(_attributesForNegativeValues);
|
|
|
|
RELEASE(_maximum);
|
|
|
|
RELEASE(_minimum);
|
|
|
|
RELEASE(_roundingBehavior);
|
|
|
|
RELEASE(_roundingBehavior);
|
|
|
|
RELEASE(_attributedStringForNil);
|
|
|
|
RELEASE(_attributedStringForNotANumber);
|
|
|
|
RELEASE(_attributedStringForZero);
|
|
|
|
[super dealloc];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (NSString*) decimalSeparator
|
|
|
|
{
|
|
|
|
if (_decimalSeparator == 0)
|
|
|
|
return @"";
|
|
|
|
else
|
|
|
|
return [NSString stringWithCharacters: &_decimalSeparator length: 1];
|
2000-09-02 01:44:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (NSString*) editingStringForObjectValue: (id)anObject
|
|
|
|
{
|
|
|
|
return [self stringForObjectValue: anObject];
|
|
|
|
}
|
|
|
|
|
2001-10-19 09:10:04 +00:00
|
|
|
- (void) encodeWithCoder: (NSCoder*)encoder
|
|
|
|
{
|
|
|
|
[encoder encodeValueOfObjCType: @encode(BOOL) at: &_hasThousandSeparators];
|
|
|
|
[encoder encodeValueOfObjCType: @encode(BOOL) at: &_allowsFloats];
|
|
|
|
[encoder encodeValueOfObjCType: @encode(BOOL) at: &_localizesFormat];
|
|
|
|
[encoder encodeValueOfObjCType: @encode(unichar) at: &_thousandSeparator];
|
|
|
|
[encoder encodeValueOfObjCType: @encode(unichar) at: &_decimalSeparator];
|
|
|
|
|
2001-11-23 12:37:33 +00:00
|
|
|
[encoder encodeObject: _roundingBehavior];
|
|
|
|
[encoder encodeObject: _maximum];
|
|
|
|
[encoder encodeObject: _minimum];
|
|
|
|
[encoder encodeObject: _attributedStringForNil];
|
|
|
|
[encoder encodeObject: _attributedStringForNotANumber];
|
|
|
|
[encoder encodeObject: _attributedStringForZero];
|
|
|
|
[encoder encodeObject: _negativeFormat];
|
|
|
|
[encoder encodeObject: _positiveFormat];
|
|
|
|
[encoder encodeObject: _attributesForPositiveValues];
|
|
|
|
[encoder encodeObject: _attributesForNegativeValues];
|
2001-10-19 09:10:04 +00:00
|
|
|
}
|
2004-09-14 03:34:37 +00:00
|
|
|
|
2001-10-19 09:10:04 +00:00
|
|
|
- (NSString*) format
|
|
|
|
{
|
|
|
|
if (_attributedStringForZero != nil)
|
|
|
|
{
|
|
|
|
return [NSString stringWithFormat: @"%@;%@;%@",
|
|
|
|
_positiveFormat, [_attributedStringForZero string], _negativeFormat];
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return [NSString stringWithFormat: @"%@;%@",
|
|
|
|
_positiveFormat, _negativeFormat];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2000-09-02 01:44:24 +00:00
|
|
|
- (BOOL) getObjectValue: (id*)anObject
|
|
|
|
forString: (NSString*)string
|
|
|
|
errorDescription: (NSString**)error
|
|
|
|
{
|
2003-06-12 03:29:35 +00:00
|
|
|
/* FIXME: This is just a quick hack implementation. */
|
2005-10-17 10:47:54 +00:00
|
|
|
NSLog(@"NSNumberFormatter-getObjectValue:forString:... not fully implemented");
|
|
|
|
|
|
|
|
/* Just assume nothing else has been setup and do a simple conversion. */
|
|
|
|
if ([self hasThousandSeparators])
|
2003-06-12 03:29:35 +00:00
|
|
|
{
|
2005-10-17 10:47:54 +00:00
|
|
|
NSRange range;
|
|
|
|
|
|
|
|
range = [string rangeOfString: [self thousandSeparator]];
|
|
|
|
if (range.length != 0)
|
|
|
|
{
|
|
|
|
string = AUTORELEASE([string mutableCopy]);
|
|
|
|
[(NSMutableString*)string replaceOccurrencesOfString: [self thousandSeparator]
|
|
|
|
withString: @""
|
|
|
|
options: 0
|
|
|
|
range: NSMakeRange(0, [string length])];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (anObject)
|
|
|
|
{
|
|
|
|
NSDictionary *locale;
|
|
|
|
|
|
|
|
locale = [NSDictionary dictionaryWithObject: [self decimalSeparator]
|
|
|
|
forKey: NSDecimalSeparator];
|
|
|
|
*anObject = [NSDecimalNumber decimalNumberWithString: string
|
|
|
|
locale: locale];
|
|
|
|
if (*anObject)
|
|
|
|
{
|
|
|
|
return YES;
|
2004-09-14 03:34:37 +00:00
|
|
|
}
|
2003-06-12 03:29:35 +00:00
|
|
|
}
|
2005-10-17 10:47:54 +00:00
|
|
|
|
2003-06-12 03:29:35 +00:00
|
|
|
return NO;
|
2000-09-02 01:44:24 +00:00
|
|
|
}
|
|
|
|
|
2001-10-19 09:10:04 +00:00
|
|
|
- (BOOL) hasThousandSeparators
|
2000-09-02 01:44:24 +00:00
|
|
|
{
|
2001-10-19 09:10:04 +00:00
|
|
|
return _hasThousandSeparators;
|
2000-09-02 01:44:24 +00:00
|
|
|
}
|
|
|
|
|
2001-10-19 09:10:04 +00:00
|
|
|
- (id) init
|
2000-09-02 01:44:24 +00:00
|
|
|
{
|
2001-12-04 08:44:42 +00:00
|
|
|
id o;
|
|
|
|
|
2001-10-19 09:10:04 +00:00
|
|
|
_allowsFloats = YES;
|
|
|
|
_decimalSeparator = '.';
|
|
|
|
_thousandSeparator = ',';
|
2001-12-04 08:44:42 +00:00
|
|
|
o = [[NSAttributedString alloc] initWithString: @""];
|
|
|
|
[self setAttributedStringForNil: o];
|
|
|
|
RELEASE(o);
|
|
|
|
o = [[NSAttributedString alloc] initWithString: @"NaN"];
|
|
|
|
[self setAttributedStringForNotANumber: o];
|
|
|
|
RELEASE(o);
|
2000-09-02 01:44:24 +00:00
|
|
|
|
2001-10-19 09:10:04 +00:00
|
|
|
return self;
|
2000-09-02 01:44:24 +00:00
|
|
|
}
|
|
|
|
|
2001-10-19 09:10:04 +00:00
|
|
|
- (id) initWithCoder: (NSCoder*)decoder
|
|
|
|
{
|
2004-10-09 15:40:23 +00:00
|
|
|
if ([decoder allowsKeyedCoding])
|
|
|
|
{
|
|
|
|
if ([decoder containsValueForKey: @"NS.allowsfloats"])
|
|
|
|
{
|
|
|
|
[self setAllowsFloats: [decoder decodeBoolForKey: @"NS.allowsfloats"]];
|
|
|
|
}
|
|
|
|
if ([decoder containsValueForKey: @"NS.decimal"])
|
|
|
|
{
|
|
|
|
[self setDecimalSeparator: [decoder decodeObjectForKey: @"NS.decimal"]];
|
|
|
|
}
|
|
|
|
if ([decoder containsValueForKey: @"NS.hasthousands"])
|
|
|
|
{
|
|
|
|
[self setHasThousandSeparators: [decoder decodeBoolForKey: @"NS.hasthousands"]];
|
|
|
|
}
|
|
|
|
if ([decoder containsValueForKey: @"NS.localized"])
|
|
|
|
{
|
|
|
|
[self setLocalizesFormat: [decoder decodeBoolForKey: @"NS.localized"]];
|
|
|
|
}
|
|
|
|
if ([decoder containsValueForKey: @"NS.max"])
|
|
|
|
{
|
|
|
|
[self setMaximum: [decoder decodeObjectForKey: @"NS.max"]];
|
|
|
|
}
|
|
|
|
if ([decoder containsValueForKey: @"NS.min"])
|
|
|
|
{
|
|
|
|
[self setMinimum: [decoder decodeObjectForKey: @"NS.min"]];
|
|
|
|
}
|
|
|
|
if ([decoder containsValueForKey: @"NS.nan"])
|
|
|
|
{
|
|
|
|
[self setAttributedStringForNotANumber: [decoder decodeObjectForKey: @"NS.nan"]];
|
|
|
|
}
|
|
|
|
if ([decoder containsValueForKey: @"NS.negativeattrs"])
|
|
|
|
{
|
|
|
|
[self setTextAttributesForNegativeValues: [decoder decodeObjectForKey: @"NS.negativeattrs"]];
|
|
|
|
}
|
|
|
|
if ([decoder containsValueForKey: @"NS.negativeformat"])
|
|
|
|
{
|
|
|
|
[self setNegativeFormat: [decoder decodeObjectForKey: @"NS.negativeformat"]];
|
|
|
|
}
|
|
|
|
if ([decoder containsValueForKey: @"NS.nil"])
|
|
|
|
{
|
|
|
|
[self setAttributedStringForNil: [decoder decodeObjectForKey: @"NS.nil"]];
|
|
|
|
}
|
|
|
|
if ([decoder containsValueForKey: @"NS.positiveattrs"])
|
|
|
|
{
|
|
|
|
[self setTextAttributesForPositiveValues: [decoder decodeObjectForKey: @"NS.positiveattrs"]];
|
|
|
|
}
|
|
|
|
if ([decoder containsValueForKey: @"NS.positiveformat"])
|
|
|
|
{
|
|
|
|
[self setPositiveFormat: [decoder decodeObjectForKey: @"NS.positiveformat"]];
|
|
|
|
}
|
|
|
|
if ([decoder containsValueForKey: @"NS.rounding"])
|
|
|
|
{
|
|
|
|
[self setRoundingBehavior: [decoder decodeObjectForKey: @"NS.rounding"]];
|
|
|
|
}
|
|
|
|
if ([decoder containsValueForKey: @"NS.thousand"])
|
|
|
|
{
|
|
|
|
[self setThousandSeparator: [decoder decodeObjectForKey: @"NS.thousand"]];
|
|
|
|
}
|
|
|
|
if ([decoder containsValueForKey: @"NS.zero"])
|
|
|
|
{
|
|
|
|
[self setAttributedStringForZero: [decoder decodeObjectForKey: @"NS.zero"]];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
[decoder decodeValueOfObjCType: @encode(BOOL) at: &_hasThousandSeparators];
|
|
|
|
[decoder decodeValueOfObjCType: @encode(BOOL) at: &_allowsFloats];
|
|
|
|
[decoder decodeValueOfObjCType: @encode(BOOL) at: &_localizesFormat];
|
|
|
|
[decoder decodeValueOfObjCType: @encode(unichar) at: &_thousandSeparator];
|
|
|
|
[decoder decodeValueOfObjCType: @encode(unichar) at: &_decimalSeparator];
|
2005-02-22 11:22:44 +00:00
|
|
|
|
2004-10-09 15:40:23 +00:00
|
|
|
[decoder decodeValueOfObjCType: @encode(id) at: &_roundingBehavior];
|
|
|
|
[decoder decodeValueOfObjCType: @encode(id) at: &_maximum];
|
|
|
|
[decoder decodeValueOfObjCType: @encode(id) at: &_minimum];
|
|
|
|
[decoder decodeValueOfObjCType: @encode(id) at: &_attributedStringForNil];
|
|
|
|
[decoder decodeValueOfObjCType: @encode(id)
|
|
|
|
at: &_attributedStringForNotANumber];
|
|
|
|
[decoder decodeValueOfObjCType: @encode(id) at: &_attributedStringForZero];
|
|
|
|
[decoder decodeValueOfObjCType: @encode(id) at: &_negativeFormat];
|
|
|
|
[decoder decodeValueOfObjCType: @encode(id) at: &_positiveFormat];
|
|
|
|
[decoder decodeValueOfObjCType: @encode(id)
|
|
|
|
at: &_attributesForPositiveValues];
|
|
|
|
[decoder decodeValueOfObjCType: @encode(id)
|
|
|
|
at: &_attributesForNegativeValues];
|
|
|
|
}
|
2001-10-19 09:10:04 +00:00
|
|
|
return self;
|
2000-09-02 01:44:24 +00:00
|
|
|
}
|
|
|
|
|
2001-10-19 09:10:04 +00:00
|
|
|
- (BOOL) isPartialStringValid: (NSString*)partialString
|
|
|
|
newEditingString: (NSString**)newString
|
|
|
|
errorDescription: (NSString**)error
|
2000-09-02 01:44:24 +00:00
|
|
|
{
|
2005-10-17 10:47:54 +00:00
|
|
|
// FIXME
|
2001-10-19 09:10:04 +00:00
|
|
|
if (newString != NULL)
|
2004-09-14 03:34:37 +00:00
|
|
|
{
|
|
|
|
*newString = partialString;
|
|
|
|
}
|
|
|
|
if (error)
|
|
|
|
{
|
2004-09-14 15:55:18 +00:00
|
|
|
*error = nil;
|
2004-09-14 03:34:37 +00:00
|
|
|
}
|
2000-09-02 01:44:24 +00:00
|
|
|
|
2001-10-19 09:10:04 +00:00
|
|
|
return YES;
|
2000-09-02 01:44:24 +00:00
|
|
|
}
|
|
|
|
|
2001-10-19 09:10:04 +00:00
|
|
|
- (BOOL) localizesFormat
|
2000-09-02 01:44:24 +00:00
|
|
|
{
|
2001-10-19 09:10:04 +00:00
|
|
|
return _localizesFormat;
|
2000-09-02 01:44:24 +00:00
|
|
|
}
|
|
|
|
|
2001-10-19 09:10:04 +00:00
|
|
|
- (NSDecimalNumber*) maximum
|
2000-09-02 01:44:24 +00:00
|
|
|
{
|
2001-10-19 09:10:04 +00:00
|
|
|
return _maximum;
|
2000-09-02 01:44:24 +00:00
|
|
|
}
|
|
|
|
|
2001-10-19 09:10:04 +00:00
|
|
|
- (NSDecimalNumber*) minimum
|
2000-09-02 01:44:24 +00:00
|
|
|
{
|
2005-02-22 11:22:44 +00:00
|
|
|
return _minimum;
|
2000-09-02 01:44:24 +00:00
|
|
|
}
|
|
|
|
|
2001-10-19 09:10:04 +00:00
|
|
|
- (NSString*) negativeFormat
|
2000-09-02 01:44:24 +00:00
|
|
|
{
|
2001-10-19 09:10:04 +00:00
|
|
|
return _negativeFormat;
|
2000-09-02 01:44:24 +00:00
|
|
|
}
|
|
|
|
|
2001-10-19 09:10:04 +00:00
|
|
|
- (NSString*) positiveFormat
|
2000-09-02 01:44:24 +00:00
|
|
|
{
|
2001-10-19 09:10:04 +00:00
|
|
|
return _positiveFormat;
|
2000-09-02 01:44:24 +00:00
|
|
|
}
|
|
|
|
|
2001-10-19 09:10:04 +00:00
|
|
|
- (NSDecimalNumberHandler*) roundingBehavior
|
2000-09-02 01:44:24 +00:00
|
|
|
{
|
2001-10-19 09:10:04 +00:00
|
|
|
return _roundingBehavior;
|
2000-09-02 01:44:24 +00:00
|
|
|
}
|
|
|
|
|
2001-10-19 09:10:04 +00:00
|
|
|
- (void) setAllowsFloats: (BOOL)flag
|
2000-09-02 01:44:24 +00:00
|
|
|
{
|
2001-10-19 09:10:04 +00:00
|
|
|
_allowsFloats = flag;
|
2000-09-02 01:44:24 +00:00
|
|
|
}
|
|
|
|
|
2001-10-19 09:10:04 +00:00
|
|
|
- (void) setAttributedStringForNil: (NSAttributedString*)newAttributedString
|
2000-09-02 01:44:24 +00:00
|
|
|
{
|
2001-10-19 09:10:04 +00:00
|
|
|
ASSIGN(_attributedStringForNil, newAttributedString);
|
2000-09-02 01:44:24 +00:00
|
|
|
}
|
|
|
|
|
2001-10-19 09:10:04 +00:00
|
|
|
- (void) setAttributedStringForNotANumber:
|
|
|
|
(NSAttributedString*)newAttributedString
|
2000-09-02 01:44:24 +00:00
|
|
|
{
|
2001-10-19 09:10:04 +00:00
|
|
|
ASSIGN(_attributedStringForNotANumber, newAttributedString);
|
2000-09-02 01:44:24 +00:00
|
|
|
}
|
|
|
|
|
2001-10-19 09:10:04 +00:00
|
|
|
- (void) setAttributedStringForZero: (NSAttributedString*)newAttributedString
|
2000-09-02 01:44:24 +00:00
|
|
|
{
|
|
|
|
ASSIGN(_attributedStringForZero, newAttributedString);
|
|
|
|
}
|
|
|
|
|
2001-10-19 09:10:04 +00:00
|
|
|
- (void) setDecimalSeparator: (NSString*)newSeparator
|
2000-09-02 01:44:24 +00:00
|
|
|
{
|
2001-10-19 09:10:04 +00:00
|
|
|
if ([newSeparator length] > 0)
|
|
|
|
_decimalSeparator = [newSeparator characterAtIndex: 0];
|
|
|
|
else
|
|
|
|
_decimalSeparator = 0;
|
2000-09-02 01:44:24 +00:00
|
|
|
}
|
|
|
|
|
2001-10-19 09:10:04 +00:00
|
|
|
- (void) setFormat: (NSString*)aFormat
|
2000-09-02 01:44:24 +00:00
|
|
|
{
|
2001-10-19 09:10:04 +00:00
|
|
|
NSRange r;
|
2000-09-02 01:44:24 +00:00
|
|
|
|
2001-10-19 09:10:04 +00:00
|
|
|
r = [aFormat rangeOfString: @";"];
|
|
|
|
if (r.length == 0)
|
|
|
|
{
|
|
|
|
[self setPositiveFormat: aFormat];
|
|
|
|
[self setNegativeFormat: [@"-" stringByAppendingString: aFormat]];
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
[self setPositiveFormat: [aFormat substringToIndex: r.location]];
|
|
|
|
aFormat = [aFormat substringFromIndex: NSMaxRange(r)];
|
|
|
|
r = [aFormat rangeOfString: @";"];
|
|
|
|
if (r.length == 0)
|
|
|
|
{
|
|
|
|
[self setNegativeFormat: aFormat];
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
RELEASE(_attributedStringForZero);
|
|
|
|
_attributedStringForZero = [[NSAttributedString alloc] initWithString:
|
|
|
|
[aFormat substringToIndex: r.location]];
|
|
|
|
[self setNegativeFormat: [aFormat substringFromIndex: NSMaxRange(r)]];
|
|
|
|
}
|
|
|
|
}
|
2000-09-02 01:44:24 +00:00
|
|
|
}
|
|
|
|
|
2001-10-19 09:10:04 +00:00
|
|
|
- (void) setHasThousandSeparators: (BOOL)flag
|
2000-09-02 01:44:24 +00:00
|
|
|
{
|
2001-10-19 09:10:04 +00:00
|
|
|
_hasThousandSeparators = flag;
|
2000-09-02 01:44:24 +00:00
|
|
|
}
|
|
|
|
|
2001-10-19 09:10:04 +00:00
|
|
|
- (void) setLocalizesFormat: (BOOL)flag
|
2000-09-02 01:44:24 +00:00
|
|
|
{
|
2001-10-19 09:10:04 +00:00
|
|
|
_localizesFormat = flag;
|
2000-09-02 01:44:24 +00:00
|
|
|
}
|
|
|
|
|
2001-10-19 09:10:04 +00:00
|
|
|
- (void) setMaximum: (NSDecimalNumber*)aMaximum
|
2000-09-02 01:44:24 +00:00
|
|
|
{
|
2001-10-19 09:10:04 +00:00
|
|
|
ASSIGN(_maximum, aMaximum);
|
2000-09-02 01:44:24 +00:00
|
|
|
}
|
|
|
|
|
2001-10-19 09:10:04 +00:00
|
|
|
- (void) setMinimum: (NSDecimalNumber*)aMinimum
|
2000-09-02 01:44:24 +00:00
|
|
|
{
|
2001-10-19 09:10:04 +00:00
|
|
|
ASSIGN(_minimum, aMinimum);
|
2000-09-02 01:44:24 +00:00
|
|
|
}
|
|
|
|
|
2001-10-19 09:10:04 +00:00
|
|
|
- (void) setNegativeFormat: (NSString*)aFormat
|
2000-09-02 01:44:24 +00:00
|
|
|
{
|
2005-10-17 10:47:54 +00:00
|
|
|
// FIXME: Should extract separators and attributes
|
2001-10-19 09:10:04 +00:00
|
|
|
ASSIGN(_negativeFormat, aFormat);
|
2000-09-02 01:44:24 +00:00
|
|
|
}
|
|
|
|
|
2001-10-19 09:10:04 +00:00
|
|
|
- (void) setPositiveFormat: (NSString*)aFormat
|
2000-09-02 01:44:24 +00:00
|
|
|
{
|
2005-10-17 10:47:54 +00:00
|
|
|
// FIXME: Should extract separators and attributes
|
2001-10-19 09:10:04 +00:00
|
|
|
ASSIGN(_positiveFormat, aFormat);
|
2000-09-02 01:44:24 +00:00
|
|
|
}
|
|
|
|
|
2001-10-19 09:10:04 +00:00
|
|
|
- (void) setRoundingBehavior: (NSDecimalNumberHandler*)newRoundingBehavior
|
2000-09-02 01:44:24 +00:00
|
|
|
{
|
2001-10-19 09:10:04 +00:00
|
|
|
ASSIGN(_roundingBehavior, newRoundingBehavior);
|
2000-09-02 01:44:24 +00:00
|
|
|
}
|
|
|
|
|
2001-10-19 09:10:04 +00:00
|
|
|
- (void) setTextAttributesForNegativeValues: (NSDictionary*)newAttributes
|
2000-09-02 01:44:24 +00:00
|
|
|
{
|
2001-10-19 09:10:04 +00:00
|
|
|
ASSIGN(_attributesForNegativeValues, newAttributes);
|
2000-09-02 01:44:24 +00:00
|
|
|
}
|
|
|
|
|
2001-10-19 09:10:04 +00:00
|
|
|
- (void) setTextAttributesForPositiveValues: (NSDictionary*)newAttributes
|
2000-09-02 01:44:24 +00:00
|
|
|
{
|
2001-10-19 09:10:04 +00:00
|
|
|
ASSIGN(_attributesForPositiveValues, newAttributes);
|
2000-09-02 01:44:24 +00:00
|
|
|
}
|
|
|
|
|
2001-10-19 09:10:04 +00:00
|
|
|
- (void) setThousandSeparator: (NSString*)newSeparator
|
2000-09-02 01:44:24 +00:00
|
|
|
{
|
2001-10-19 09:10:04 +00:00
|
|
|
if ([newSeparator length] > 0)
|
|
|
|
_thousandSeparator = [newSeparator characterAtIndex: 0];
|
|
|
|
else
|
|
|
|
_thousandSeparator = 0;
|
2000-09-02 01:44:24 +00:00
|
|
|
}
|
|
|
|
|
2001-10-19 09:10:04 +00:00
|
|
|
- (NSString*) stringForObjectValue: (id)anObject
|
2000-09-02 01:44:24 +00:00
|
|
|
{
|
2005-10-17 10:47:54 +00:00
|
|
|
NSMutableDictionary *locale;
|
|
|
|
|
2001-10-19 09:10:04 +00:00
|
|
|
if (nil == anObject)
|
|
|
|
return [[self attributedStringForNil] string];
|
2000-09-02 01:44:24 +00:00
|
|
|
|
2005-10-17 10:47:54 +00:00
|
|
|
/* FIXME: This is just a quick hack implementation. */
|
|
|
|
NSLog(@"NSNumberFormatter-stringForObjectValue:... not fully implemented");
|
|
|
|
|
|
|
|
locale = [NSMutableDictionary dictionaryWithCapacity: 3];
|
|
|
|
if ([self hasThousandSeparators])
|
|
|
|
{
|
|
|
|
[locale setObject: [self thousandSeparator] forKey: NSThousandsSeparator];
|
|
|
|
}
|
|
|
|
|
|
|
|
if ([self allowsFloats])
|
|
|
|
{
|
|
|
|
[locale setObject: [self decimalSeparator] forKey: NSDecimalSeparator];
|
|
|
|
// Should also set: NSDecimalDigits
|
|
|
|
}
|
|
|
|
|
|
|
|
return [anObject descriptionWithLocale: locale];
|
2000-09-02 01:44:24 +00:00
|
|
|
}
|
|
|
|
|
2001-10-19 09:10:04 +00:00
|
|
|
- (NSDictionary*) textAttributesForNegativeValues
|
2000-09-02 01:44:24 +00:00
|
|
|
{
|
2001-10-19 09:10:04 +00:00
|
|
|
return _attributesForNegativeValues;
|
2000-09-02 01:44:24 +00:00
|
|
|
}
|
|
|
|
|
2001-10-19 09:10:04 +00:00
|
|
|
- (NSDictionary*) textAttributesForPositiveValues
|
2000-09-02 01:44:24 +00:00
|
|
|
{
|
2001-10-19 09:10:04 +00:00
|
|
|
return _attributesForPositiveValues;
|
2000-09-02 01:44:24 +00:00
|
|
|
}
|
|
|
|
|
2001-10-19 09:10:04 +00:00
|
|
|
- (NSString*) thousandSeparator
|
2000-09-02 01:44:24 +00:00
|
|
|
{
|
2005-10-17 10:47:54 +00:00
|
|
|
if (!_thousandSeparator)
|
2001-10-19 09:10:04 +00:00
|
|
|
return @"";
|
|
|
|
else
|
|
|
|
return [NSString stringWithCharacters: &_thousandSeparator length: 1];
|
2000-09-02 01:44:24 +00:00
|
|
|
}
|
2001-05-31 22:39:16 +00:00
|
|
|
|
|
|
|
@end
|