Variant changed to another type
64-bitThe value of a variant as a string, a double, a single, an integer, a long or a boolean.
integer = round(n); double = n
A variant converted to the single type keeps its full 64-bit value; it is not narrowed to 32 bits. Conversion to integer or long rounds halves toward positive infinity and does not check the 16-bit or 32-bit range. A string that does not start with a number gives nil.
d('VLAX-VARIANT-CHANGE-TYPE', (a) => {
arity(a, 2);
if (!(a[0] instanceof Variant)) throw new LispError('bad argument type: variantp ' + show(a[0]));
const to = intv(a[1]), v = a[0].value;
if (to === VT.STRING) return new Variant(to, isNum(v) ? (isInt(v) ? String(v.v) : String(v)) : show(v, false));
if (to === VT.DOUBLE || to === VT.SINGLE) { const n = isNum(v) ? numVal(v) : typeof v === 'string' ? parseFloat(v) : NaN; return Number.isFinite(n) ? new Variant(to, n) : null; }
if (to === VT.INTEGER || to === VT.LONG) { const n = isNum(v) ? numVal(v) : typeof v === 'string' ? parseFloat(v) : NaN; return Number.isFinite(n) ? new Variant(to, int(Math.round(n))) : null; }
if (to === VT.BOOLEAN) return new Variant(to, truthy(v) && v !== FALSE && !(isNum(v) && numVal(v) === 0) ? TRUE : FALSE);
return new Variant(to, v);
});