How to Get the Decimal Part of a Number in TypeScript?

Have you ever needed to extract just the decimal part from a number in your TypeScript project? Maybe you’re building a financial dashboard or processing sales tax calculations, and you need to isolate the cents from a dollar amount.

In this tutorial, I’ll walk you through several methods to get the decimal (fractional) part of a number in TypeScript. We’ll explore built-in JavaScript methods, some basic math, and even how to handle negative numbers.

Below are the methods:

Method 1: Using the Modulo Operator and Math.floor()

The quickest way I’ve found to get the decimal part of a positive number in TypeScript is by subtracting the integer part from the original value. Here’s how you can do it:

const amount = 123.45;
const decimalPart = amount - Math.floor(amount);
console.log(decimalPart); // Output: 0.45

This is how it works:

  • Math.floor(amount) returns the largest integer less than or equal to amount (for example, 123).
  • Subtracting this from the original number leaves you with just the decimal part (0.45).

You can also write like this:

const amount = 123.45;
const decimalPart = amount - Math.floor(amount);

// Round to 2 decimal places (common for cents)
const roundedDecimalPart = Number(decimalPart.toFixed(2));

console.log(roundedDecimalPart); // Output: 0.45

This method works perfectly for positive numbers, which is most common in US currency and measurement scenarios.

Check out How to Format Numbers with Commas in TypeScript

Method 2: Using the Modulo Operator with 1

Another very concise method is to use the modulo operator (%). In JavaScript and TypeScript, number % 1 gives you the fractional part.

Here is an example.

const price = 19.99;
const cents = price % 1;
console.log(cents); // Output: 0.99

This method works for both positive and negative numbers. However, for negative numbers, the result will also be negative (e.g., -3.75 % 1 gives -0.75). If you want the absolute decimal part, you can use Math.abs():

const negativeAmount = -47.68;
const decimal = Math.abs(negativeAmount % 1);
console.log(decimal); // Output: 0.68

Read How to Format Currency to 2 Decimals in TypeScript

Method 3: Using Number Methods and String Manipulation

Sometimes, you might want to get the decimal part as a string, especially if you’re formatting output for display (e.g., showing cents in a different color). Here’s how you can do that:

const amount = 45.67;
const decimalString = amount.toString().split('.')[1] || '00';
console.log(decimalString); // Output: "67"

How it works:

  • Convert the number to a string.
  • Split at the decimal point.
  • The second part of the array is your decimal part as a string.

You can see the exact output in the screenshot below:

Get the Decimal Part of a Number in TypeScript

This is handy for UI work, such as displaying the cents in a price separately.

Check out How to Convert Number to String with Leading Zeros in TypeScript?

Method 4: Handle Floating-Point Precision

If you’re working with financial data, floating-point precision can sometimes cause unexpected results. For example, 0.1 + 0.2 in JavaScript/TypeScript is 0.30000000000000004. To avoid this, you can round the decimal part to a fixed number of digits:

const value = 0.1 + 0.2;
const decimalPart = +(value - Math.floor(value)).toFixed(2);
console.log(decimalPart); // Output: 0.3

Tip:
Always use toFixed() for display or when you need precise decimal places, especially in financial applications.

Here is the exact output in the screenshot below:

TypeScript Get the Decimal Part of a Number

Read How to Round Down to 2 Decimals in TypeScript?

Method 5: Extract the Decimal Part in Percentage Calculations

Let’s look at a real-world example: Suppose you’re calculating the sales tax for a product in California (where the base rate is 7.25%). You want to show just the cents from the tax calculation.

const price = 59.99;
const taxRate = 0.0725;
const tax = price * taxRate;
const decimalCents = +(tax % 1).toFixed(2);
console.log(`Cents from tax: $${decimalCents}`); // Output: Cents from tax: $0.34

This method is especially useful in US e-commerce or point-of-sale systems.

Handle Negative Numbers

As mentioned earlier, using % 1 on negative numbers returns a negative decimal. If you always want a positive decimal part, wrap the result with Math.abs().

Below is the TypeScript code:

const negativeValue = -12.56;
const positiveDecimal = Math.abs(negativeValue % 1);
console.log(positiveDecimal); // Output: 0.56

In this tutorial, I explained how to get the decimal Part of a Number in TypeScript using various methods.

  • For numbers you control (positive/financial): Use number % 1 or number - Math.floor(number).
  • For formatting or UI: Use string manipulation.
  • For negative numbers: Use Math.abs() to ensure a positive decimal.
  • For floating-point accuracy: Use toFixed().

You may also like the following tutorials:

Power Apps functions free pdf

30 Power Apps Functions

This free guide walks you through the 30 most-used Power Apps functions with real business examples, exact syntax, and results you can see.

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