Skip to content

Commit bf0ac0a

Browse files
committed
Fix sig fig check for large number when decimal is present
Fixes #30
1 parent ac295cf commit bf0ac0a

File tree

2 files changed

+8
-3
lines changed

2 files changed

+8
-3
lines changed

JsonConverter.bas

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -468,6 +468,7 @@ End Function
468468
Private Function json_ParseNumber(json_String As String, ByRef json_Index As Long) As Variant
469469
Dim json_Char As String
470470
Dim json_Value As String
471+
Dim json_IsLargeNumber As Boolean
471472

472473
json_SkipSpaces json_String, json_Index
473474

@@ -483,8 +484,10 @@ Private Function json_ParseNumber(json_String As String, ByRef json_Index As Lon
483484
' This can lead to issues when BIGINT's are used (e.g. for Ids or Credit Cards), as they will be invalid above 15 digits
484485
' See: http://support.microsoft.com/kb/269370
485486
'
486-
' Fix: Parse -> String, Convert -> String longer than 15 characters containing only numbers and decimal points -> Number
487-
If Not JsonOptions.UseDoubleForLargeNumbers And Len(json_Value) >= 16 Then
487+
' Fix: Parse -> String, Convert -> String longer than 15/16 characters containing only numbers and decimal points -> Number
488+
' (decimal doesn't factor into significant digit count, so if present check for 15 digits + decimal = 16)
489+
json_IsLargeNumber = IIf(InStr(json_Value, "."), Len(json_Value) >= 17, Len(json_Value) >= 16)
490+
If Not JsonOptions.UseDoubleForLargeNumbers And json_IsLargeNumber Then
488491
json_ParseNumber = json_Value
489492
Else
490493
' VBA.Val does not use regional settings, so guard for comma is not needed

specs/Specs.bas

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,12 +96,14 @@ Public Function Specs() As SpecSuite
9696
End With
9797

9898
With Specs.It("should handle very long numbers as strings (e.g. BIGINT)")
99-
JsonString = "[123456789012345678901234567890, 1.123456789012345678901234567890]"
99+
JsonString = "[123456789012345678901234567890, 1.123456789012345678901234567890, 123456789012345, 1.23456789012345]"
100100
Set JsonObject = JsonConverter.ParseJson(JsonString)
101101

102102
.Expect(JsonObject).ToNotBeUndefined
103103
.Expect(JsonObject(1)).ToEqual "123456789012345678901234567890"
104104
.Expect(JsonObject(2)).ToEqual "1.123456789012345678901234567890"
105+
.Expect(JsonObject(3)).ToEqual 123456789012345#
106+
.Expect(JsonObject(4)).ToEqual 1.23456789012345
105107

106108
JsonConverter.JsonOptions.UseDoubleForLargeNumbers = True
107109
JsonString = "[123456789012345678901234567890]"

0 commit comments

Comments
 (0)