Type conversion functions¶
These functions convert values of one type into another type, if possible. All of these functions take a single argument of arbitrary type, and return the result of the type corresponding to the name of the function.
bool(x)¶
Converts its argument into a boolean value.
If
xis already a boolean, then it returns the argument as-is.If
xis numeric, then the result isfalsewhenxis0, andtruefor all other values ofx.If
xis string, then the function will check whether that string can be found withinYarnProject.trueValuesorYarnProject.falseValuessets. If yes, then it will return thetrue/falsevalue respectively. Otherwise, an error will be thrown.
number(x)¶
Converts its argument x into a numeric value.
If
xis boolean, then it returns1fortrueand0forfalse.If
xis numeric, then it is returned unmodified.If
xis string, then the function attempts to parse that string as a number. A runtime exception will be raised ifxdoes not have a valid format for a number. The following formats are recognized:integer:
"-3","214"decimal:
"0.745","3.14159",".1","-3."scientific:
"2e5","3.11e-05"hexadecimal:
"0xDEAD","0x7F"
string(x)¶
Converts its argument x into a string value.
If
xis boolean, returns strings"true"or"false".If
xis numeric, converts it into a string representation using the standard Dart’s.toString()method, which attempts to produce the shortest string that can represent the numberx. In particular,if
xis integer-valued, returns its decimal representation without a decimal point;if
xis a double in the range1e-6to1e21, returns its decimal representation with a decimal point;for all other doubles, returns
xwritten in the scientific (exponential) format.
If
xis a string, then it is returned as-is.