Diferencia entre revisiones de «Separadores numéricos»

De Wiki Proyectos Beta
Ir a la navegación Ir a la búsqueda
([feat] Add information about JavaScript ES12 - Separadores numéricos)
 
([feat] Add information about JavaScript ES12 - Separadores numéricos)
Línea 3: Línea 3:
= Ejemplos =
= Ejemplos =


  // Decimal integer literal with digits grouped by thousand.
  // A billion.
  let n1 = 1_000_000_000;
  const n1 = 1_000_000_000;
  console.log(n1); // This will print: 1000000000
  console.log(n1); // This will print: 1000000000
// Hundreds of millions.   
const n2 = 1_475_938.38;
console.log(n2); // This will print: 1475938.38


  // Decimal literal with digits grouped by thousand.
  // Decimal literal with digits grouped by thousand.
  let n2 = 1_000_000_000.150_200
  let n3 = 1_000_000_000.150_200
  console.log(n2); // This will print: 1000000000.1502
  console.log(n3); // This will print: 1000000000.1502


  // Hexadecimal integer literal with digits grouped by byte.
  // Hexadecimal integer literal with digits grouped by byte.
  let n3 = 0x95_65_98_FA_A9
  let n4 = 0x95_65_98_FA_A9
  console.log(n3); // This will print: 641654651561
  console.log(n4); // This will print: 641654651561


  // BigInt literal with digits grouped by thousand.
  // BigInt literal with digits grouped by thousand.
  let n4 = 155_326_458_156_248_168_514n
  let n45 = 155_326_458_156_248_168_514n
  console.log(n4); // This will print: 155326458156248168514n
  console.log(n5); // This will print: 155326458156248168514n

Revisión del 11:35 13 ago 2022

Los separadores numéricos le permiten agregar guiones bajos entre dígitos en números literales, lo que los hace más legibles. Estos guiones bajos se eliminarán automáticamente cuando se analicen los archivos.

Ejemplos

// A billion.
const n1 = 1_000_000_000;
console.log(n1); // This will print: 1000000000
// Hundreds of millions.    
const n2 = 1_475_938.38;
console.log(n2); // This will print: 1475938.38
// Decimal literal with digits grouped by thousand.
let n3 = 1_000_000_000.150_200
console.log(n3); // This will print: 1000000000.1502
// Hexadecimal integer literal with digits grouped by byte.
let n4 = 0x95_65_98_FA_A9
console.log(n4); // This will print: 641654651561
// BigInt literal with digits grouped by thousand.
let n45 = 155_326_458_156_248_168_514n
console.log(n5); // This will print: 155326458156248168514n