The secret to wholesale...

In the article about buying Thai goods, I introduced wholesale markets and cheap...

Restaurant business and the...

Customers are a special figure in restaurants. This may seem obvious, but...

Guide the clues to...

Japanese goods Known for putting quality first. However, in the past, due...

Online cosmetics business and...

Starting a business, especially online cosmetics business is the silver trend and...

Có thể sử dụng toString để chuyển đổi entero và cadena de texto



Bản gốc Artículo: JavaScript Number to String – Cách sử dụng toString để chuyển đổi một số nguyên thành một chuỗi bởi Nathan Sebhastian


Traducido bởi: Miguel Rendon

phương pháp toString() là một phương pháp tích hợp đối tượng Number de JavaScript que le allowe convertir cualquier valor de tipo numérico en su repación de tipo cadena.

Sử dụng phương thức toString trong JavaScript

Para usar el method toString()simplemente necesitas llamar al method en un valor numérico. El siguiente ejemplo muestra hasmo convertir el valor numérico 24 en su đại diện cho cadena. Quan sát cómo el valor de la variable str está entre comillas nhân đôi:

var num = 24;
var str = num.toString();

console.log(num); // 24
console.log(str); // "24"
Convertir un number a cadena with the method toString()

También puedes llamar al phương pháp toString() inmediatamente en un valor numéricopero debes agregar paréntesis () para envolver el valor o nuestro JavaScript replya con un error de token no válido o inesperado.

phương pháp toString() también puedes convertir number of flotantes y negativos como se muestra a continuación:

24.toString(); //token no válido o inesperado 
(24).toString(); // "24"
(9.7).toString(); // "9.7"
(-20).toString(); // "-20"
Convierta cualquier tipo de numberer with the method toString()

Cuối cùng, phương pháp toString() también acepta el parámetro radix o base. La base cho phép chuyển đổi một số hệ thống số thập phân (cơ số 10) và một cadena que đại diện cho một số trong hệ thống số khác.

Los sistemas numéricos válidos para la conversión incluyen:

  • Hệ thống nhị phân (cơ sở 2) que tiene 2 dígitos, 0 y 1
  • Hệ thống ternario (cơ sở 3) que tiene 3 dígitos 0, 1 y 2
  • Hệ thống cuaternario (cơ sở 4) que tiene 4 dígitos, 0, 1, 2 y 3
  • Y así hasta el sistema Hệ thập lục phân (cơ sở 36) que tiene la combinación de los numbereros arábigos del 0 al 9 y las letras latinas de la A a la Z
Đọc thêm  Difficulties in starting a clean food business
Number.toString(radix);
Hàm sintaxis cho phương thức toString(), chấp nhận cơ số tham số

Los parametros de base aceptan datos de tipo numérico con valores que van del 2 tất cả 36. Đây là một ví dụ về bộ chuyển đổi có số thập phân 5 đại diện su của số nhị phân (cơ sở 2):

var str = (5).toString(2);

console.log(str); // "101"
Chuyển đổi số thập phân thành số nhị phân theo phương thức toString()

Số thập phân 5 del código anterior se convierte en su số nhị phân tương đương a 101 y luego se convierte en una cadena.

Có thể sử dụng các mẹo dữ liệu khác với phương thức toString()

Además de convertir el tipo de númerophương pháp toString() también está disponible para convertir otros tipos de data en tusđại diện của cadena.

Ví dụ, puedes convertir un tipo de array en su đại diện cadena de la siguiente manera:

var arr = [ "Nathan", "Jack" ];
var str = arr.toString();

console.log(str); // "Nathan,Jack"
Convierta un array en una cadena with the method toString()

O un tipo booleano một cadena como se muestra a continuación:

var bool = true;
var str = bool.toString();

console.log(str); // "true"

Pero creo que usarás with thị trưởng frecuencia el method toString() chuyển đổi cho un número en una cadena en lugar de los deás. Eso es lo que suelo hacer también 🙂

Gracias por leer este hướng dẫn



Zik.vn – Biên dịch & Biên soạn Lại

spot_img

Create a website from scratch

Just drag and drop elements in a page to get started with Newspaper Theme.

Buy Now ⟶

Bài viết liên quang

DMCA.com Protection Status