Example: cmd: -b:js -d:nodejs --experimental:strictFuncs
import src/nodejs/jsstrmisc func example() = assert parseFloatThousandSep("0.0") == 0.0 assert parseFloatThousandSep("1.0") == 1.0 assert parseFloatThousandSep("-0.0") == -0.0 assert parseFloatThousandSep("-1.0") == -1.0 assert parseFloatThousandSep("1.000") == 1.0 assert parseFloatThousandSep("1.000") == 1.0 assert parseFloatThousandSep("-1.000") == -1.0 assert parseFloatThousandSep("-1,222.0001") == -1222.0001 assert parseFloatThousandSep("3.141592653589793") == 3.141592653589793 assert parseFloatThousandSep("6.283185307179586") == 6.283185307179586 assert parseFloatThousandSep("2.718281828459045") == 2.718281828459045 assert parseFloatThousandSep("10,000,000.000") == 10000000.0 assert parseFloatThousandSep("1,222.0001") == 1222.0001 assert parseFloatThousandSep("10.000,0", {}, '.', ',') == 10000.0 assert parseFloatThousandSep("1'000'000,000", {}, '\'', ',') == 1000000.0 assert parseFloatThousandSep("1000000", {pfDotOptional}) == 1000000.0 assert parseFloatThousandSep("-1,000", {pfDotOptional}) == -1000.0 assert parseFloatThousandSep("1000,000", {pfDotOptional}) == 1000000.0 assert parseFloatThousandSep(".1", {pfLeadingDot}) == 0.1 assert parseFloatThousandSep("1", {pfDotOptional}) == 1.0 assert parseFloatThousandSep("1.", {pfTrailingDot}) == 1.0 assert parseFloatThousandSep("10,0.0,0,0", {pfSepAnywhere}) == 100.0 assert parseFloatThousandSep("01.00") == 1.0 assert parseFloatThousandSep("1,000.000e-9") == 1e-06 assert parseFloatThousandSep("10,00.0", {pfSepAnywhere}) == 1000.0 assert parseFloatThousandSep("0", {pfDotOptional}) == 0.0 assert parseFloatThousandSep("-0", {pfDotOptional}) == -0.0 assert parseFloatThousandSep("1,111", {pfDotOptional}) == 1111.0 assert parseFloatThousandSep(".1", {pfLeadingDot}) == 0.1 assert parseFloatThousandSep("1.", {pfTrailingDot}) == 1.0 assert parseFloatThousandSep(".1", {pfLeadingDot,pfTrailingDot}) == 0.1 assert parseFloatThousandSep("1.", {pfLeadingDot,pfTrailingDot}) == 1.0 assert parseFloatThousandSep("1", {pfDotOptional}) == 1.0 assert parseFloatThousandSep("1.0,0,0", {pfSepAnywhere}) == 1.0 assert parseFloatThousandSep(".10", {pfLeadingDot}) == 0.1 assert parseFloatThousandSep("10.", {pfTrailingDot}) == 10.0 assert parseFloatThousandSep("10", {pfDotOptional, pfSepAnywhere}) == 10.0 assert parseFloatThousandSep("1.0,0,0,0,0,0,0,0", {pfSepAnywhere}) == 1.0 assert parseFloatThousandSep("0,0,0,0,0,0,0,0.1", {pfSepAnywhere}) == 0.1 assert parseFloatThousandSep("1.0e9") == 1000000000.0 assert parseFloatThousandSep("1.0e-9") == 1e-09 assert parseFloatThousandSep("1,000.000e9") == 1000000000000.0 assert parseFloatThousandSep("1e9", {pfDotOptional}) == 1000000000.0 assert parseFloatThousandSep("1.0E9") == 1000000000.0 assert parseFloatThousandSep("1.0E-9") == 1e-09 assert parseFloatThousandSep("Inf", {pfNanInf}) == Inf assert parseFloatThousandSep("-Inf", {pfNanInf}) == -Inf assert parseFloatThousandSep("+Inf", {pfNanInf}) == +Inf assert parseFloatThousandSep("1000.000000E+90") == 1e93 assert parseFloatThousandSep("-10 000 000 000.0001", sep=' ') == -10000000000.0001 assert parseFloatThousandSep("-10 000 000 000,0001", sep=' ', decimalDot = ',') == -10000000000.0001 doAssertRaises(ValueError): discard parseFloatThousandSep(" ", {pfDotOptional}) doAssertRaises(ValueError): discard parseFloatThousandSep(".1.", {pfLeadingDot,pfTrailingDot}) doAssertRaises(ValueError): discard parseFloatThousandSep("1ee9", {pfDotOptional}) doAssertRaises(ValueError): discard parseFloatThousandSep("aNa", {pfNanInf}) doAssertRaises(ValueError): discard parseFloatThousandSep("fnI", {pfNanInf}) doAssertRaises(ValueError): discard parseFloatThousandSep("1,000.000,000,E,+,9,0", {pfSepAnywhere}) for s in ["1,11", "1,1", "1,0000.000", "--", "..", "1,,000", "1..000", "1,000000", ",1", "1,", "1.", ".1", "10,00.0", "1,000.000ee9", "1e02.2", "1.0e--9", "Inf", "-Inf", "+Inf", "NaN"]: doAssertRaises(ValueError): discard parseFloatThousandSep(s) example() static: example()
Types
ParseFloatOptions = enum pfLeadingDot, ## Allow leading dot, like ".9" and similar. pfTrailingDot, ## Allow trailing dot, like "9." and similar. pfSepAnywhere, ## Allow separator anywhere in between, like "9,9", "9,99". pfDotOptional, ## Allow "9", "-0", integer literals, etc. pfNanInf ## Allow "NaN", "Inf", "-Inf", etc.
- Options for parseFloatThousandSep.
Procs
func parseFloatThousandSep(str: openArray[char]; options: set[ParseFloatOptions] = {}; sep = ','; decimalDot = '.'): float {....raises: [ValueError], tags: [].}
-
Convenience func for parseFloat which allows for thousand separators, this is designed to parse floats as found in the wild formatted for humans.
Fine grained flexibility and strictness is up to the user, you can set the options using ParseFloatOptions enum.
parseFloatThousandSep "prepares" str and then calls parseFloat.
The following assumptions and requirements must be met:
- str must not be empty string.
- str must be stripped of trailing and leading whitespaces.
- sep and decimalDot must be different.
- sep must not be in {'-', '+', 'e', 'i', 'n', 'f', 'a', '\n'}.
- decimalDot must not be in {'-', '+', 'e', 'i', 'n', 'f', 'a', ' ', '\t', '\v', '\c', '\n', '\f'}.