Skip to content
This repository was archived by the owner on Jan 3, 2019. It is now read-only.

Commit 583a8fb

Browse files
committedJan 10, 2015
[#462] fix for fiat amount parsing on Yosemite
1 parent c818092 commit 583a8fb

File tree

3 files changed

+64
-2
lines changed

3 files changed

+64
-2
lines changed
 

‎Hive.xcodeproj/project.pbxproj

+5-1
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,7 @@
223223
58AE8CBB19226AF900946C97 /* icon-success.png in Resources */ = {isa = PBXBuildFile; fileRef = 58AE8CB919226AF800946C97 /* icon-success.png */; };
224224
58AE8CBC19226AF900946C97 /* icon-success@2x.png in Resources */ = {isa = PBXBuildFile; fileRef = 58AE8CBA19226AF800946C97 /* icon-success@2x.png */; };
225225
58AE8CC9192CCF1200946C97 /* HIGeneralPreferencesViewController.strings in Resources */ = {isa = PBXBuildFile; fileRef = 58AE8CCB192CCF1200946C97 /* HIGeneralPreferencesViewController.strings */; };
226+
58B83ABE1A60B5BF00A9172D /* NSDecimalNumberSpec.m in Sources */ = {isa = PBXBuildFile; fileRef = 58B83ABD1A60B5BF00A9172D /* NSDecimalNumberSpec.m */; };
226227
58C4E84719B0BD53003B5171 /* HITransactionPopoverViewController.xib in Resources */ = {isa = PBXBuildFile; fileRef = 58C4E84919B0BD53003B5171 /* HITransactionPopoverViewController.xib */; };
227228
58C4E86819B0BDD3003B5171 /* HITransactionPopoverViewController.strings in Resources */ = {isa = PBXBuildFile; fileRef = 58C4E86A19B0BDD3003B5171 /* HITransactionPopoverViewController.strings */; };
228229
58C85B5818FFE90700B2C350 /* HISignMessageWindowController.xib in Resources */ = {isa = PBXBuildFile; fileRef = 58C85B5A18FFE90700B2C350 /* HISignMessageWindowController.xib */; };
@@ -1856,6 +1857,7 @@
18561857
58AE8CE3192CCF2C00946C97 /* pt */ = {isa = PBXFileReference; lastKnownFileType = text.plist.strings; name = pt; path = pt.lproj/HIGeneralPreferencesViewController.strings; sourceTree = "<group>"; };
18571858
58AE8CE4192CCF2D00946C97 /* ro */ = {isa = PBXFileReference; lastKnownFileType = text.plist.strings; name = ro; path = ro.lproj/HIGeneralPreferencesViewController.strings; sourceTree = "<group>"; };
18581859
58AE8CE5192CCF2D00946C97 /* hu */ = {isa = PBXFileReference; lastKnownFileType = text.plist.strings; name = hu; path = hu.lproj/HIGeneralPreferencesViewController.strings; sourceTree = "<group>"; };
1860+
58B83ABD1A60B5BF00A9172D /* NSDecimalNumberSpec.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = NSDecimalNumberSpec.m; sourceTree = "<group>"; };
18591861
58C4E84819B0BD53003B5171 /* en */ = {isa = PBXFileReference; lastKnownFileType = file.xib; name = en; path = en.lproj/HITransactionPopoverViewController.xib; sourceTree = "<group>"; };
18601862
58C4E84A19B0BD5A003B5171 /* pl */ = {isa = PBXFileReference; lastKnownFileType = file.xib; name = pl; path = pl.lproj/HITransactionPopoverViewController.xib; sourceTree = "<group>"; };
18611863
58C4E84B19B0BD61003B5171 /* fr */ = {isa = PBXFileReference; lastKnownFileType = file.xib; name = fr; path = fr.lproj/HITransactionPopoverViewController.xib; sourceTree = "<group>"; };
@@ -2530,8 +2532,9 @@
25302532
isa = PBXGroup;
25312533
children = (
25322534
24EAAFC8186E1BBD00E1D2D3 /* HIBitcoinFormatServiceSpec.m */,
2533-
24EAAFC3186E1BBD00E1D2D3 /* Supporting Files */,
25342535
1AA107B12E9AB06FCF455005 /* HICurrencyFormatServiceSpec.m */,
2536+
58B83ABD1A60B5BF00A9172D /* NSDecimalNumberSpec.m */,
2537+
24EAAFC3186E1BBD00E1D2D3 /* Supporting Files */,
25352538
);
25362539
path = HiveTests;
25372540
sourceTree = "<group>";
@@ -3477,6 +3480,7 @@
34773480
isa = PBXSourcesBuildPhase;
34783481
buildActionMask = 2147483647;
34793482
files = (
3483+
58B83ABE1A60B5BF00A9172D /* NSDecimalNumberSpec.m in Sources */,
34803484
24EAAFC9186E1BBD00E1D2D3 /* HIBitcoinFormatServiceSpec.m in Sources */,
34813485
1AA10DECEE3B99EDD1C14B81 /* HICurrencyFormatServiceSpec.m in Sources */,
34823486
);
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,28 @@
11
#import "NSDecimalNumber+HISatoshiConversion.h"
22

3+
static NSDecimalNumberHandler *_satoshiRoundingBehavior;
4+
35
@implementation NSDecimalNumber(HISatoshiConversion)
46

7+
+ (void)load {
8+
// workaround for a NSDecimalNumber bug in Yosemite
9+
_satoshiRoundingBehavior = [NSDecimalNumberHandler decimalNumberHandlerWithRoundingMode:NSRoundPlain
10+
scale:0
11+
raiseOnExactness:NO
12+
raiseOnOverflow:NO
13+
raiseOnUnderflow:NO
14+
raiseOnDivideByZero:NO];
15+
}
16+
517
+ (NSDecimalNumber *)hiDecimalNumberWithSatoshi:(satoshi_t)satoshi {
618
return [NSDecimalNumber decimalNumberWithMantissa:ABS(satoshi)
719
exponent:-8
820
isNegative:satoshi < 0];
921
}
1022

1123
- (satoshi_t)hiSatoshi {
12-
return [self decimalNumberByMultiplyingByPowerOf10:8].unsignedLongLongValue;
24+
NSDecimalNumber *satoshiAmount = [self decimalNumberByMultiplyingByPowerOf10:8];
25+
return [[satoshiAmount decimalNumberByRoundingAccordingToBehavior:_satoshiRoundingBehavior] unsignedLongLongValue];
1326
}
1427

1528
@end

‎HiveTests/NSDecimalNumberSpec.m

+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
//
2+
// NSDecimalNumberSpec.m
3+
// Hive
4+
//
5+
// Created by Jakub Suder on 10/01/15.
6+
// Copyright (c) 2015 Hive Developers. All rights reserved.
7+
//
8+
9+
#import "NSDecimalNumber+HISatoshiConversion.h"
10+
11+
SPEC_BEGIN(NSDecimalNumberSpec)
12+
13+
describe(@"hiSatoshi", ^{
14+
it(@"should return the amount in satoshi", ^{
15+
NSDecimalNumber *num = [NSDecimalNumber decimalNumberWithString:@"25"];
16+
17+
assertThatUnsignedLongLong([num hiSatoshi], is(equalToUnsignedLongLong(2500000000)));
18+
});
19+
20+
context(@"for numbers with a fractional part", ^{
21+
it(@"should return a correct value", ^{
22+
NSDecimalNumber *num = [NSDecimalNumber decimalNumberWithString:@"6.251"];
23+
24+
assertThatUnsignedLongLong([num hiSatoshi], is(equalToUnsignedLongLong(625100000)));
25+
});
26+
});
27+
28+
context(@"for numbers with fractions of satoshi", ^{
29+
it(@"should return a rounded value", ^{
30+
NSDecimalNumber *num = [NSDecimalNumber decimalNumberWithString:@"11.234200989"];
31+
32+
assertThatUnsignedLongLong([num hiSatoshi], is(equalToUnsignedLongLong(1123420099)));
33+
});
34+
});
35+
36+
context(@"for numbers with a very long fractional part", ^{
37+
it(@"should return a rounded value", ^{
38+
NSDecimalNumber *num = [NSDecimalNumber decimalNumberWithString:@"3045.001122334455667788"];
39+
40+
assertThatUnsignedLongLong([num hiSatoshi], is(equalToUnsignedLongLong(304500112233)));
41+
});
42+
});
43+
});
44+
45+
SPEC_END

0 commit comments

Comments
 (0)
This repository has been archived.