★ wanayoo — archive 1999 https://developer.mozilla.org/ca/docs/Web/JavaScript/Referencia/Objectes_globals/NumberNouvelle recherche | Portail wanayoo
mozilla
Your Search Results

    Number

    This article is in need of an editorial review.

    Resum

    L'objecte de JavaScript Number és un objecte embolcall que permet treballar amb valors numèrics. Un objecte de tipus Number és creat quan es crida el constructor Number().

    Constructor

    new Number(valor);

    Paràmetres

    valor
    El valor numèric de l'objecte que es pretèn crear.

    Descripció

    Els usos pricipals de l'objecte Number són:

    • Si l'argument dona al constructor no es pot convertir en un nombre aquest retorna NaN.
    • Number pot ser emprat per a realitzar conversions de tipus sense necesitat de crear un nou objecte.

    Propietats

    Number.EPSILON
    L'interval més petit entre dos nombres que es pot representar.
    Number.MAX_SAFE_INTEGER
    El nombre sencer més gran que JavaScript pot representar de forma segura (253 - 1).
    Number.MAX_VALUE
    El nombre positiu més gran que pot ésser representat.
    Number.MIN_SAFE_INTEGER
    El nombre més petit que JavaScript pot representar de forma segura (-(253 - 1)).
    Number.MIN_VALUE
    El nombre positiu més petit que pot ésser representat. És a dir, el nombre positiu més proper a zero sense arribar a ser zero.
    Number.NaN
    El valor especial "not a number".
    Number.NEGATIVE_INFINITY
    El valor especial que representa l'infinit negatiu. És retornat quan es produeix un overflow.
    Number.POSITIVE_INFINITY
    El valor especial que representa l'infinit positiu. És retornat quan es produeix un overflow.
    Number.prototype
    Permet l'adició de propietats a l'objecte Number.
    Properties inherited from Function:

    Mètodes

    For methods available on Number instances, see Methods of Number instances.
    Number.isNaN()
    Determina si el valor proporcionat és NaN.
    Number.isFinite()
    Determina si el valor que representa és un valor finit.
    Number.isInteger()
    Determina si el valor que representa és un nombre i si es tracta d'un nombre sencer.
    Number.isSafeInteger()
    Determina si el tipus del valor proporcionat així com el valor en sí mateix és un nombre sencer segur (nombre entre -(253 - 1) i 253 - 1).
    Number.toInteger()
    Utilitzat per evaluar el valor passat i convertir-lo a sencer (o bé Infinity), però s'ha eliminat.
    Number.parseFloat()
    Fa la mateixa funció que el mètode parseFloat de l'objecte global.
    Number.parseInt()
    Fa la mateixa funció que el mètode parseInt de l'objecte global.
    Methods inherited from Function:

    Instàncies de Number

    Totes les instàncies de Number hereten de Number.prototype. L'objecte prototype del constructor Number pot ser modificat per a afectar a totes les instàncies de Number.

    Mètodes

    Number.prototype.toExponential()
    Returns a string representing the number in exponential notation.
    Number.prototype.toFixed()
    Returns a string representing the number in fixed-point notation.
    Number.prototype.toLocaleString()
    Returns a string with a language sensitive representation of this number. Overrides the Object.prototype.toLocaleString() method.
    Number.prototype.toPrecision()
    Returns a string representing the number to a specified precision in fixed-point or exponential notation.
    Number.prototype.toSource()
    Returns an object literal representing the specified Number object; you can use this value to create a new object. Overrides the Object.prototype.toSource() method.
    Number.prototype.toString()
    Returns a string representing the specified object in the specified radix (base). Overrides the Object.prototype.toString() method.
    Number.prototype.valueOf()
    Returns the primitive value of the specified object. Overrides the Object.prototype.valueOf() method.

    Exemples

    Exemple: Utilitzar l'objecte Number per a assignar valors numèrics a variables

    L'exemple que trobareu a continuació utilitza les propietats de l'objecte Number per a assignar valors a diferents variables numèriques:

    var biggestNum = Number.MAX_VALUE;
    var smallestNum = Number.MIN_VALUE;
    var infiniteNum = Number.POSITIVE_INFINITY;
    var negInfiniteNum = Number.NEGATIVE_INFINITY;
    var notANum = Number.NaN;
    

    Exemple: Rang de sencers de Number

    El següent exemple mostra el valor sencer mínim i màxim que pot ésser representat per un objecte de tipus Number (per a més detalls, referiu-vos a l'standard EcmaScript, capítol 8.5 El tipus Number):

    var biggestInt = 9007199254740992;
    var smallestInt = -9007199254740992;
    

    A l'interpretar dades que han sigut serialitzades amb JSON, els valors que queden fora d'aquest rang poden esdevenir corruptes quan l'intèrpret de JSON els forci esdevenir de tipus Number. Una forma d'evitar aquesta limitació és utilitzar una String per representar el nombre.

    Exemple: Utilitzar Number per a convertir un objecte de tipus Date

    L'exemple que trobareu a continuació converteix un objecte de tipus Date a un tipus numèric tot utilitzant Number com una funció:

    var d = new Date('December 17, 1995 03:24:00');
    print(Number(d));
    

    Aquest exemple mostrarà "819199440000".

    Exemple: Convertir cadenes de caràcters numèriques a nombres

    Number("123")     // 123
    Number("")        // 0
    Number("0x11")    // 17
    Number("0b11")    // 3
    Number("0o11")    // 9
    Number("foo")     // NaN
    Number("100a")    // NaN
    

    Especificacions

    Especificació Estat Comentaris
    ECMAScript 1a Edició. Standard Definició inicial. Implementat a JavaScript 1.1.
    ECMAScript 5.1 (ECMA-262)
    The definition of 'Number' in that specification.
    Standard  
    ECMAScript 6 (ECMA-262)
    The definition of 'Number' in that specification.
    Release Candidate Afegides nous mètodes i propietats: (EPSILON, isFinite, isInteger, isNaN, parseFloat, parseInt)

    Compatibilitat amb navegadores

    Característica Chrome Firefox (Gecko) Internet Explorer Opera Safari
    Suport bàsic (Yes) (Yes) (Yes) (Yes) (Yes)
    Característica Android Chrome for Android Firefox Mobile (Gecko) IE Mobile Opera Mobile Safari Mobile
    Suport bàsic (Yes) (Yes) (Yes) (Yes) (Yes) (Yes)

    Vegeu també

    Document Tags and Contributors

    Contributors to this page: enTropy, teoli, Sheppy
    Last updated by: teoli,
    Hide Sidebar