Category talk:Wren-str: Difference between revisions
Content added Content deleted
(Bug fixed properly now!) |
(→Source code: Added Str.toNum method.) |
||
Line 595: | Line 595: | ||
var chars = ca.map { |c| String.fromCodePoint(c) }.toList |
var chars = ca.map { |c| String.fromCodePoint(c) }.toList |
||
return (count < 1000) ? Strs.concat_(chars) : Strs.concat(chars, 1000) |
return (count < 1000) ? Strs.concat_(chars) : Strs.concat(chars, 1000) |
||
} |
|||
// After trimming whitespace from the string 's', takes as many characters as possible |
|||
// to form a valid number and converts it thereto using the Num.fromString method. |
|||
// Returns null if such a conversion is impossible. |
|||
static toNum(s) { |
|||
if (s is Num) return s |
|||
if (!(s is String)) s = "%(s)" |
|||
s = s.trim() |
|||
var n = Num.fromString(s) |
|||
if (n) return n |
|||
if (s.count < 2) return null |
|||
var chars = s.toList |
|||
for (i in chars.count-1..1) { |
|||
chars.removeAt(i) |
|||
if (n = Num.fromString(chars.join())) return n |
|||
} |
|||
return null |
|||
} |
} |
||
} |
} |