-
Notifications
You must be signed in to change notification settings - Fork 0
/
NSDecimalNumber+RoundingExtensions.m
49 lines (36 loc) · 1.66 KB
/
NSDecimalNumber+RoundingExtensions.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
//
// NSDecimalNumber+RoundingExtensions.m
// Property Manager
//
// Created by Samuel Williams on 19/02/12.
// Copyright (c) 2012 Orion Transfer Ltd. All rights reserved.
//
#import "NSDecimalNumber+RoundingExtensions.h"
@implementation NSDecimalNumber (RoundingExtensions)
- (NSDecimalNumber *)decimalNumberByFlooring {
static NSDecimalNumberHandler * behavior = nil;
if (!behavior) {
behavior = [NSDecimalNumberHandler decimalNumberHandlerWithRoundingMode:NSRoundDown scale:0 raiseOnExactness:NO raiseOnOverflow:NO raiseOnUnderflow:NO raiseOnDivideByZero:NO];
}
return [self decimalNumberByRoundingAccordingToBehavior:behavior];
}
- (NSDecimalNumber *)decimalNumberByCeiling {
static NSDecimalNumberHandler * behavior = nil;
if (!behavior) {
behavior = [NSDecimalNumberHandler decimalNumberHandlerWithRoundingMode:NSRoundUp scale:0 raiseOnExactness:NO raiseOnOverflow:NO raiseOnUnderflow:NO raiseOnDivideByZero:NO];
}
return [self decimalNumberByRoundingAccordingToBehavior:behavior];
}
- (NSDecimalNumber *)fractionalPart {
return [self decimalNumberBySubtracting:self.decimalNumberByFlooring];
}
+ (NSDecimalNumber *) decimalNumberWithUnsignedInteger:(NSUInteger)integer {
return [[self.class alloc] initWithUnsignedInteger:integer];
}
+ (NSDecimalNumber *) decimalNumberWithNumerator:(NSUInteger)numerator denominator:(NSUInteger)denominator {
return [[self decimalNumberWithUnsignedInteger:numerator] decimalNumberByDividingBy:[self decimalNumberWithUnsignedInteger:denominator]];
}
- (NSDecimalNumber *) decimalNumberByNegating {
return [self decimalNumberByMultiplyingBy:[NSDecimalNumber decimalNumberWithMantissa:1 exponent:0 isNegative:YES]];
}
@end