Typescript Float type with examples

This tutorial explains how to declare and assign float values in TypeScript.

TypeScript language provides the following types to store numeric values:

  • number type to store primitive values.
  • Number type to store objects.

Numeric numbers can contain values such as:

  • Integers such as -2, 0, -1.
  • Float numbers such as 1.68, 7.45.

Typescript float type

A float is a numeric value with decimals or fractions. It is declared using normal variable syntax and assigned floating-point values.

let price: number = 1.23;

let totalCost: number;
totalCost = 2 * number;

Check if a Given Number is a Float or Not

You can determine if a number is a float by using the remainder operator (%) with 1.

const checkForFlat = (input: number): boolean => {
  return value % 1 !== 0;
};
console.log(checkForFlat(2.9)); // return true
console.log(checkForFlat(11.0)); // false 
console.log(checkForFlat(3)); // false