How to convert string to double in typescript

In this typescript tutorial, we will see how to convert string to double in typescript, using different methods.

Below are the 3 methods to convert string to double in typescript:

  • Using the number() constructor
  • Using the parseFloat method
  • Using the + operator

For example, if the string is “21.44”, we will convert it to double then it returns 21.44(without quotes).

Note that in typescript integers, floating point numbers and double-precision floating point numbers is coming under the number data type.

Convert string to double using number() constructor

Here we will see how we can convert the string to double in typescript using the number() constructor.

The number () constructor in typescript is used to convert the string to a number

For example, the string “22,44444” is passed to the number constructor, then it converts it to a parse floating point number.

In the code editor, create a file i.e. stringToDouble.ts, and then write the below code.

let strVal:string = "22.44444";
let numVal:number = Number(strVal);
console.log(numVal); // Output: 22.44444

To compile the below code, run the below command and you can see the result in the console:

ts-node stringToDouble.ts
 convert the string to double in typescript using the number() constructor.
convert the string to double in typescript using the number() constructor.

This is an example of converting the string to double in typescript using the number() constructor.

Convert string to double in typescript using parseFloat method

Here we will see how we can convert the string to double in typescript using the parseFloat method.

See also  Typescript Split String

The parseFloat() in typescript is used to parse the string and return the parse floating point number.

For example, we will take a string like below and by using parseFloat(), convert it into parse floating point number or double

In the ‘stringToDouble.ts’ file write the below code:

let strVal:string = "22.44444";
let numVal :number = parseFloat(strVal);
console.log(numVal); // Output:22.44444

To compile the below code, run the below command and you can see the result in the console:

ts-node stringToDouble.ts
 convert the string to double in typescript using the parseFloat method.
convert the string to double in typescript using the parseFloat method.

This is an example of converting the string to double in typescript using the parseFloat method.

Convert string to double in typescript using + operator

Here we will see how we can convert the string to double in typescript using the + operator.

The + operator in typescript is convert the string into a number. when we apply +operator to a string, it converted the string to a number.

For example when we add + operator to the ‘22.44444’, then it converted to 22.44444.

In the ‘stringToDouble.ts’ file write the below code:

let strVal:string = "22.44444";
let numVal:number = +strVal;
console.log(numVal); // Output: 22.44444

To compile the code run the below command and you can see the result in the console.

ts-node stringToDouble.ts
convert the string to double in typescript using the + operator.
convert the string to double in typescript using the + operator.

This is an example of converting the string to double in typescript using the + operator.

Conclusion

In this typescript tutorial, we saw how to convert the string to double in typescript using different methods. These are:

  • Using the number() constructor
  • Using the parseFloat method
  • Using the + operator

You may also like:

>