gnustep-dev
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Test base library stable branch please


From: David Ayers
Subject: Re: Test base library stable branch please
Date: Wed, 03 Jan 2007 01:48:07 +0100
User-agent: Mozilla Thunderbird 1.0.2 (X11/20061113)

Graham J Lee schrieb:
> On 2 Jan 2007, at 10:16, David Ayers wrote:
> 
>>
>> I'm not sure if I fully agree here.  I would have expected
>> @"1.234,57" which of course would have also failed the test.  So yes,
>> the test cases need to honor localisation, yet the formatting does not
>> produce the expected result.
>>
>> Let me know if you need me to debug this.
> 
> Yes please.  It passes on my system (obviously, or I wouldn't have 
> committed the patch ;-)) and I can't work out how to end up with a 
> decimal point but no decimal places, given the default format  string. 
> I think the reason the thousands separator and decimal place  are
> non-localised is a bug in -[NSNumberFormatter init], which I  haven't
> addressed.  Shouldn't be too hard to fix though.
> 

OK, then... I haven't wrapped my mind around the documentation or
implementation of the formatter yet but it seems this is an issue with
NSDecimalNumber and has nothing to do with the formatter.

The issue seems to be in NSDecimalNumber.m :
/**
 * Inefficient ... quick hack by converting double value to string,
 * then initializing from string.
 */
- (id) initWithBytes: (const void*)value objCType: (const char*)type
{
  double        tmp;
  NSString      *s;

  memcpy(&tmp, value, sizeof(tmp));
  s = [[NSString alloc] initWithFormat: @"%g", tmp];
  self = [self initWithString: s];
  RELEASE(s);
  return self;
}
where s = @"1234.57"
and initWithString: uses
GSPrivateDefaultLocale:
{... NSDecimalSeparator = ","; ... NSThousandsSeparator = "";... }
which was correctly extracted from the locale information.

So ...
  s = [[NSString alloc] initWithFormat: @"%g", tmp];
... is returning @"1234.57".

If I printf("###:%g\n",tmp) I do get
###:1234,57

So let's look at initWithFormat: and it is documented to pass a nil
locale which implies non-localized format, i.e. it is in fact doing the
correct thing.  NSDecimalNumber's initWithString: OTOH doesn't mention
how it handles the locale but our implementation uses the default locale
as determined by GSPrivateDefaultLocale().

So my best guess is to improve the "hack" by calling:
  s = [[NSString alloc] initWithFormat: @"%g"
                        locale: GSPrivateDefaultLocale(), tmp];

But maybe someone wants do the honors of implemeting a more direct
double->decimal conversion... (I'll might look into it if people think
it's worthwhile).

Cheers,
David

PS:
PASS: +[NSNumberFormatter alloc] returns a NSNumberFormatter
PASS: default format same as Cocoa
PASS: round up for fractional part >0.5
PASS: round down for fractional part <0.5
PASS: numeric and space padding OK
PASS: prefix and suffix used properly
PASS: negativeFormat used for -ve number
PASS: notANumber special case
PASS: format string of length 1




reply via email to

[Prev in Thread] Current Thread [Next in Thread]