How To Convert String To Double In TypeScript

Recently, I was building a financial dashboard application where I needed to convert string inputs to double values for calculations.

The challenge with TypeScript is that it doesn’t have a specific “double” type like some other languages. Instead, we use the number type, which can represent both integers and floating-point values.

In this TypeScript tutorial, we will explore how to convert a string to a double in TypeScript using various methods.

Below are the three methods to convert a string to a double in TypeScript:

  • Using the Unary Plus (+) Operator
  • Using the Number() Constructor
  • Using parseFloat()

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

Note that in TypeScript, integers, floating-point numbers, and double-precision floating-point numbers fall under the number data type.

Understanding Types in TypeScript

Let me first help you to understand that TypeScript doesn’t have a specific “double” type. Instead, TypeScript uses:

  • number: Represents both integers and floating-point values
  • string: Represents textual data

When we talk about converting to “double” in TypeScript, we’re talking about converting to the number type with decimal precision.

Convert String To Double Using Number() Constructor

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

The number () constructor in TypeScript is used to convert a 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 a double in TypeScript using the Number () constructor.

This is an example of converting the string to a double in TypeScript using the Number () constructor.

Here is another example:

function convertWithNumberConstructor(str: string): number {
    return Number(str);
}

// Usage examples
const amount = convertWithNumberConstructor("199.99");  // Returns 199.99
const negativeValue = convertWithNumberConstructor("-45.67");  // Returns -45.67
const invalid = convertWithNumberConstructor("12.34.56");  // Returns NaN

Check out Get String After Character In TypeScript

Convert A String To A Double in TypeScript Using parseFloat() Method

Here we will see how to convert the string to a double in TypeScript using the parseFloat() method.

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

For example, we will take a string like below and by using parseFloat(), convert it into a 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.

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

Here is another example:

function convertWithParseFloat(str: string): number {
    return parseFloat(str);
}

// Usage examples
const taxRate = convertWithParseFloat("7.25%");  // Returns 7.25
const measurement = convertWithParseFloat("42.5 inches");  // Returns 42.5
const invalid = convertWithParseFloat("dollars 99.95");  // Returns NaN

Check out TypeScript Check If String Is Number

Convert A String To A Double in TypeScript Using + Operator

Now, let me show you how to convert a string to a double in TypeScript using the + operator.

The + operator in TypeScript converts the string into a number. When we apply the + operator to a string, it converts 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 command below, and you can see the result in the console.

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

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

Let me show you another code and example.

function convertWithUnaryPlus(str: string): number {
    return +str;
}

// Usage examples
const price = convertWithUnaryPlus("23.45");  // Returns 23.45
const temperature = convertWithUnaryPlus("-3.8");  // Returns -3.8
const invalid = convertWithUnaryPlus("$29.99");  // Returns NaN

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:

Download User registration canvas app

DOWNLOAD USER REGISTRATION POWER APPS CANVAS APP

Download a fully functional Power Apps Canvas App (with Power Automate): User Registration App

Power Platform Tutorial

FREE Power Platform Tutorial PDF

Download 135+ Pages FREE PDF on Microsoft Power Platform Tutorial. Learn Now…