2014-01-21 13:51:54 +00:00
|
|
|
#import "ObjectTesting.h"
|
|
|
|
#import <Foundation/NSAutoreleasePool.h>
|
|
|
|
#import <Foundation/NSPropertyList.h>
|
|
|
|
#import <Foundation/NSValue.h>
|
|
|
|
#import <Foundation/NSDecimalNumber.h>
|
|
|
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <limits.h>
|
|
|
|
|
|
|
|
int main()
|
|
|
|
{
|
|
|
|
START_SET("NSDecimalNumber")
|
|
|
|
NSDecimalNumberHandler *handler;
|
|
|
|
NSDecimalNumber *n1;
|
|
|
|
NSDecimalNumber *n2;
|
|
|
|
NSString *s1;
|
|
|
|
NSString *s2;
|
2016-03-08 17:16:09 +00:00
|
|
|
NSLocale *locale;
|
2014-01-21 13:51:54 +00:00
|
|
|
|
2016-03-08 17:16:09 +00:00
|
|
|
locale = [NSLocale systemLocale];
|
2014-01-21 13:51:54 +00:00
|
|
|
handler = [NSDecimalNumberHandler alloc];
|
|
|
|
handler = [[handler initWithRoundingMode: NSRoundPlain
|
|
|
|
scale: 2
|
|
|
|
raiseOnExactness: NO
|
|
|
|
raiseOnOverflow: NO
|
|
|
|
raiseOnUnderflow: NO
|
|
|
|
raiseOnDivideByZero: NO] autorelease];
|
|
|
|
|
|
|
|
s1 = [NSString stringWithFormat: @"%0.2f", 0.009];
|
2016-03-08 17:16:09 +00:00
|
|
|
n1 = [NSDecimalNumber decimalNumberWithString: @"0.009" locale: locale];
|
2014-01-21 13:51:54 +00:00
|
|
|
n2 = [n1 decimalNumberByRoundingAccordingToBehavior: handler];
|
2016-03-08 17:16:09 +00:00
|
|
|
s2 = [n2 descriptionWithLocale: locale];
|
2014-01-21 14:01:47 +00:00
|
|
|
PASS_EQUAL(s2, s1, "rounding 0.009");
|
2014-01-21 13:51:54 +00:00
|
|
|
|
2014-01-21 14:01:47 +00:00
|
|
|
s1 = [NSString stringWithFormat: @"%0.2f", 0.019];
|
2016-03-08 17:16:09 +00:00
|
|
|
n1 = [NSDecimalNumber decimalNumberWithString: @"0.019" locale: locale];
|
2014-01-21 14:01:47 +00:00
|
|
|
n2 = [n1 decimalNumberByRoundingAccordingToBehavior: handler];
|
2016-03-08 17:16:09 +00:00
|
|
|
s2 = [n2 descriptionWithLocale: locale];
|
2014-01-21 14:01:47 +00:00
|
|
|
PASS_EQUAL(s2, s1, "rounding 0.019");
|
|
|
|
|
|
|
|
handler = [NSDecimalNumberHandler alloc];
|
|
|
|
handler = [[handler initWithRoundingMode: NSRoundPlain
|
|
|
|
scale: 3
|
|
|
|
raiseOnExactness: NO
|
|
|
|
raiseOnOverflow: NO
|
|
|
|
raiseOnUnderflow: NO
|
|
|
|
raiseOnDivideByZero: NO] autorelease];
|
|
|
|
|
|
|
|
s1 = [NSString stringWithFormat: @"%0.3f", 0.0009];
|
2016-03-08 17:16:09 +00:00
|
|
|
n1 = [NSDecimalNumber decimalNumberWithString: @"0.0009" locale: locale];
|
2014-01-21 14:01:47 +00:00
|
|
|
n2 = [n1 decimalNumberByRoundingAccordingToBehavior: handler];
|
2016-03-08 17:16:09 +00:00
|
|
|
s2 = [n2 descriptionWithLocale: locale];
|
2014-01-21 14:01:47 +00:00
|
|
|
PASS_EQUAL(s2, s1, "rounding 0.0009");
|
|
|
|
|
|
|
|
s1 = [NSString stringWithFormat: @"%0.3f", 0.0019];
|
2016-03-08 17:16:09 +00:00
|
|
|
n1 = [NSDecimalNumber decimalNumberWithString: @"0.0019" locale: locale];
|
2016-03-08 09:13:13 +00:00
|
|
|
/* Try working with NSDecimal directly
|
|
|
|
*/
|
|
|
|
{
|
|
|
|
NSDecimal result;
|
|
|
|
NSDecimal d1 = [n1 decimalValue];
|
|
|
|
NSString *s2;
|
|
|
|
|
|
|
|
NSLog(@"NSDecimal before rounding %g", NSDecimalDouble(&d1));
|
|
|
|
NSDecimalRound(&result, &d1, [handler scale], [handler roundingMode]);
|
|
|
|
NSLog(@"NSDecimal after rounding %g", NSDecimalDouble(&result));
|
|
|
|
s2 = NSDecimalString(&result, nil);
|
|
|
|
PASS_EQUAL(s2, s1, "NSDecimal rounding 0.0019 to 0.02");
|
|
|
|
n2 = [NSDecimalNumber decimalNumberWithDecimal: result];
|
|
|
|
}
|
2016-03-08 17:16:09 +00:00
|
|
|
s2 = [n2 descriptionWithLocale: locale];
|
2016-03-08 09:13:13 +00:00
|
|
|
PASS_EQUAL(s2, s1, "NSDecimalNumber rounding 0.0019");
|
2014-01-21 13:51:54 +00:00
|
|
|
|
|
|
|
END_SET("NSDecimalNumber")
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|