Quirrel4.41.0

types.Float.constructor

Method on every float
constructor(...): any

Converts value to a float. This is what runs when types.Float itself is called, types.Float(value).

Return value

0.0 (printed as "0", see tostring) when called with no arguments. Otherwise value converted to a float: a float is returned as is, an integer is widened, a bool gives 0.0 or 1.0, and a string is parsed as a decimal number - unlike types.Integer.constructor, there is no base argument, since float literals have no base-16 or base-8 form.

Errors

Throws cannot convert to Float for a table, array, null or any other non-numeric, non-string, non-bool type, and cannot convert string to Float when the string does not parse as a number.

Notes

Takes zero or one argument, but the VM shows it with the same generic fallback receiver described on types.String.constructor; extra arguments beyond the first are accepted and ignored.

Example

examples/types/float/constructor.nut
let types = require("types")
println("types.Float() =", types.Float())        // no args: 0.0, printed with no decimal point
println("types.Float(5) =", types.Float(5))       // integer -> float
println("types.Float(\"3.25\") =", types.Float("3.25"))  // string parse
try { types.Float(null) }
catch (e) println("types.Float(null) throws:", e)
Output:
types.Float() = 0
types.Float(5) = 5
types.Float("3.25") = 3.25
types.Float(null) throws: cannot convert to Float

See also

tofloatReturns the float itself.
constructorConverts value to an integer. This is what runs when types.Integer itself is called, types.Integer(value).
constructorConverts value to a bool. This is what runs when types.Bool itself is called, types.Bool(value).
floatclass index