-
Notifications
You must be signed in to change notification settings - Fork 0
/
lib_str_to_num.ks
61 lines (52 loc) · 1.39 KB
/
lib_str_to_num.ks
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
// This file is distributed under the terms of the MIT license, (c) the KSLib team
@LAZYGLOBAL off.
local num_lex is lexicon().
num_lex:add("0", 0).
num_lex:add("1", 1).
num_lex:add("2", 2).
num_lex:add("3", 3).
num_lex:add("4", 4).
num_lex:add("5", 5).
num_lex:add("6", 6).
num_lex:add("7", 7).
num_lex:add("8", 8).
num_lex:add("9", 9).
function str_to_num {
parameter s.
// Handle negative numbers
if s:startswith("-") {
return str_to_num(s:substring(1,s:length-1)) * -1.
}
// Scientific Notation
local e is s:find("e").
if e <> -1 {
local m is s:substring(e+1,1).
if m <> "+" and m <> "-" { return "NaN". }
local p is s:split("e" + m).
if p:length <> 2 { return "NaN". }
local p0 is str_to_num(p[0]).
local p1 is str_to_num(p[1]).
if p0 = "NaN" or p1 = "NaN" { return "NaN". }
if m = "+" {
return p0 * 10^p1.
} else {
return (p0 / 10^p1).
}
}
// Decimals
if s:contains(".") {
local p is s:split(".").
if p:length <> 2 { return "NaN". }
local p0 is str_to_num(p[0]).
local p1 is str_to_num(p[1]).
if p0 = "NaN" or p1 = "NaN" { return "NaN". }
return p0 + (p1 / (10^p[1]:length)).
}
// Integers (match on tokens, and bit-shift)
local v is 0.
for i IN s:split(""):sublist(1,s:length) {
if num_lex:haskey(i) { set v to v + num_lex[i]. } else { return "NaN". }
set v TO v * 10.
}
return v / 10.
}