-
Notifications
You must be signed in to change notification settings - Fork 2
/
basic_extras_ParseFirstFloat.bas
93 lines (86 loc) · 5.82 KB
/
basic_extras_ParseFirstFloat.bas
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
'--------------------------------------------------------------------------------------------------'
' ParseFirstFloat '
'--------------------------------------------------------------------------------------------------'
' Returns first not separated by non-whitespace chars float nubmers. '
' '
' Parameters: '
' '
' Str As String '
' String to parse. '
' '
' Optional CheckNegatives As Boolean <Default = TRUE> '
' Ignore the minus sign and parse only numbers. '
' '
' Examples: '
'--------------------------------------------------------------------------------------------------'
' '
' numbers = ParseFirstFloat(str) '
' '
' Expected values: '
' str: "Price is 2425 , 93 abcd 4223" numbers: "2425,93" '
' str: "Price is 2425. abcd 93 efgh 4223" numbers: "2425" '
' str: ".2. abcd 93 efgh 4223" numbers: "2" '
' str: "Price. 0.45" numbers: "0.45" '
' str: "Price. 0.45 3" numbers: "0.453" '
' str: "Price. 0.-45" numbers: "0" '
' str: "12.34.56.78" numbers: "12.34" '
' str: "44." numbers: "44" '
' str: "1 .45" numbers: "1.45" '
' str: ".45" numbers: "45" '
' str: "-.45" numbers: "-45" '
' str: ".-45" numbers: "-45" '
' str: ".4-5" numbers: "4" '
' str: "Price. -45" numbers: "-45" '
'--------------------------------------------------------------------------------------------------'
' '
' numbers = ParseFirstFloat(str, FALSE) '
' '
' Expected values: '
' str: "Price. -45" numbers: "45" '
'--------------------------------------------------------------------------------------------------'
' Feedback & Issues: '
' https://github.com/aa6/libreoffice_calc_basic_extras/issues '
'--------------------------------------------------------------------------------------------------'
Function ParseFirstFloat(Str As String, Optional CheckNegatives As Boolean) As String
Dim pos As Long
Dim poschar As String
Dim decimal_separators As String
Dim numbers_are_negative As Boolean
If IsMissing(CheckNegatives) Then
CheckNegatives = TRUE
End If
ParseFirstFloat = ""
decimal_separators = ".,"
numbers_are_negative = FALSE
For pos = 1 To Len(Str)
poschar = Mid(Str, pos, 1)
If CheckNegatives AND poschar = "-" AND ParseFirstFloat = "" Then
numbers_are_negative = TRUE
GoTo ParseFirstFloatNextPos
End If
If Instr(decimal_separators, poschar) AND ParseFirstFloat <> "" Then
ParseFirstFloat = ParseFirstFloat & poschar
decimal_separators = ""
GoTo ParseFirstFloatNextPos
End If
If Instr("0123456789", poschar) <> 0 Then
ParseFirstFloat = ParseFirstFloat & poschar
Else
If NOT (Instr(Chr(32) + Chr(160), poschar) <> 0) AND NOT (Len(ParseFirstFloat) = 0) Then
GoTo ParseFirstFloatExitPos
End If
End If
ParseFirstFloatNextPos:
Next pos
ParseFirstFloatExitPos:
If Len(ParseFirstFloat) = 0 Then
Exit Function
End If
' Trim trailing decimal_separators. "45." --> "45" '
If Instr("0123456789", Mid(ParseFirstFloat, Len(ParseFirstFloat), 1)) = 0 Then
ParseFirstFloat = Mid(ParseFirstFloat, 1, Len(ParseFirstFloat) - 1)
End If
If numbers_are_negative Then
ParseFirstFloat = "-" & ParseFirstFloat
End If
End Function