Mozilla.com

Table of contents
  1. 1. Syntax
  2. 2. Description
  3. 3. See Also
Table of contents
  1. 1. Syntax
  2. 2. Description
  3. 3. See Also

Convert any JavaScript value to a floating-point number of type jsdouble.

Syntax

JSBool JS_ValueToNumber(JSContext *cx, jsval v, jsdouble *dp);
Name Type Description
cx JSContext * The context in which to perform the conversion. Requires request. (In a JS_THREADSAFE build, the caller must be in a request on this JSContext.)
v jsval The value to convert.
dp jsdouble * Out parameter. On success, *dp receives the converted floating-point value.

Description

JS_ValueToNumber converts a JavaScript value to a number. It implements the ToNumber operator described in ECMA 262-3 §9.3 .

  • If v is already a number, conversion succeeds.
  • If v is undefined, null, false, or true, conversion succeeds, and the result is NaN, 0, 0, or 1 respectively.
  • If v is a string, conversion proceeds as specified in ECMA 262-3 §9.3.1 . JS_ValueToNumber always succeeds in this case, returning JS_TRUE. If the string is not a numeric string, the jsdouble dp out parameter is set to NaN.
  • If v is an object, conversion is successful if the object's JSClass.convert callback returns a primitive value that can be converted.

On success, JS_ValueToNumber stores the converted value in *dp and returns JS_TRUE. On error or exception, it returns JS_FALSE, and the value left in *dp is undefined.

Page last modified 20:07, 5 Sep 2008 by Jorend

Files (0)