feat: add Num.fromString static method to parse decimal string literals

Implements a new `fromString` static method on the `Num` class that attempts to parse a string as a decimal number literal using `strtod`. Returns the parsed `Num` value on success, or `null` if the string does not represent a valid number. Includes runtime validation that the argument is a string, and adds test coverage for positive, negative, zero, and decimal inputs as well as non-numeric strings.
This commit is contained in:
Gavin Schulz
2015-02-23 04:06:17 +00:00
parent 708b883e1f
commit 87cc5fdbae
4 changed files with 34 additions and 2 deletions
+8
View File
@@ -0,0 +1,8 @@
IO.print(Num.fromString("123") == 123) // expect: true
IO.print(Num.fromString("-123") == -123) // expect: true
IO.print(Num.fromString("-0") == -0) // expect: true
IO.print(Num.fromString("12.34") == 12.34) // expect: true
IO.print(Num.fromString("-0.0001") == -0.0001) // expect: true
// Test a non-number literal and ensure it returns null.
IO.print(Num.fromString("test1") == null) // expect: true
+1 -1
View File
@@ -6,5 +6,5 @@ IO.print(-0) // expect: -0
IO.print(123.456) // expect: 123.456
IO.print(-0.001) // expect: -0.001
// TODO: Hex? Scientific notation?
// TODO: Scientific notation?
// TODO: Literals at and beyond numeric limits.