@@ -14,7 +14,8 @@ extension String: PostgreSQLDataConvertible {
14
14
var ndigits : Int16
15
15
/// How many of the digits are before the decimal point (always add 1)
16
16
var weight : Int16
17
- /// If 1, this number is negative. Otherwise, positive.
17
+ /// If 0x4000, this number is negative. See NUMERIC_NEG in
18
+ /// https://github.com/postgres/postgres/blob/master/src/backend/utils/adt/numeric.c
18
19
var sign : Int16
19
20
/// The number of sig digits after the decimal place (get rid of trailing 0s)
20
21
var dscale : Int16
@@ -41,7 +42,7 @@ extension String: PostgreSQLDataConvertible {
41
42
value = value. advanced ( by: 2 )
42
43
}
43
44
44
- /// conver the current char to its string form
45
+ /// convert the current char to its string form
45
46
let string : String
46
47
if char == 0 {
47
48
/// 0 means 4 zeros
@@ -52,12 +53,20 @@ extension String: PostgreSQLDataConvertible {
52
53
53
54
/// depending on our offset, append the string to before or after the decimal point
54
55
if offset < metadata. weight. bigEndian + 1 {
56
+ // insert zeros (skip leading)
57
+ if offset > 0 {
58
+ integer += String ( repeating: " 0 " , count: 4 - string. count)
59
+ }
55
60
integer += string
56
61
} else {
57
- // Leading zeros matter with fractional
58
- fractional += fractional . count == 0 ? String ( repeating: " 0 " , count: 4 - string. count) + string : string
62
+ // leading zeros matter with fractional
63
+ fractional += String ( repeating: " 0 " , count: 4 - string. count) + string
59
64
}
60
65
}
66
+
67
+ if integer. count == 0 {
68
+ integer = " 0 "
69
+ }
61
70
62
71
if fractional. count > metadata. dscale. bigEndian {
63
72
/// use the dscale to remove extraneous zeroes at the end of the fractional part
@@ -74,7 +83,7 @@ extension String: PostgreSQLDataConvertible {
74
83
}
75
84
76
85
/// use sign to determine adding a leading `-`
77
- if metadata. sign. bigEndian == 1 {
86
+ if ( metadata. sign. bigEndian & 0x4000 ) != 0 {
78
87
return " - " + numeric
79
88
} else {
80
89
return numeric
0 commit comments