JavaScript Math trunc: How to Removes Decimals

javascript math trunc

JavaScript gives you the Math.trunc() function to work with decimal numbers. You use it when you want to remove the decimal part and keep only the whole number.

This helps when you want quick cuts without rounding. Developers use Math.trunc to chop float values cleanly and directly.

Understand JavaScript Math.trunc()

Math.trunc() removes the decimal part of a number. It keeps everything before the dot. It does not round, and it just slices off what comes after the decimal.

Here is the syntax:

Math.trunc(number)

The function takes one number. It gives back the integer part. The original value stays the same.

JavaScript skips all rounding rules here. It does not care if the decimal is .1 or .9. It just drops it.

Here is a quick look:

Math.trunc(7.9)

This gives you 7. It cuts off the .9 and keeps the whole number only.

Math.trunc(7.1)

This still gives you 7. It just drops the .1 with no change to the base.

Here are use cases:

  • Strip float values for fixed display.
  • Force integer output without rounding.
  • Remove decimal noise in calculations.
  • Handle pixel values in layout math.
  • Make score or value checks simple.

So, how does Math.trunc() really behave?

1 – It removes the decimal:

The function cuts the number after the dot. That is it. No checking. No changing.

2 – It works with negatives too:

Math.trunc keeps the sign. It still removes the decimal.

Math.trunc(-5.7) // -5
Math.trunc(-2.2) // -2

3 – No rounding, ever:

It does not matter if the decimal is .9 or .99999 — the function never moves the number up or down.

Examples of Math.trunc() in JavaScript

Cut float values:

Math.trunc(8.6) // 8  
Math.trunc(3.3) // 3

Handle negative numbers:

Math.trunc(-4.9) // -4  
Math.trunc(-0.1) // 0

Edge cases:

Math.trunc(0.9)  // 0  
Math.trunc(-0.9) // 0  
Math.trunc(0)    // 0  

Zero and near-zero values behave as expected. The decimal vanishes. The sign stays if it exists.

Browser Support for Math.trunc()

You can use Math.trunc() in most modern browsers. It works out of the box in recent versions. You get solid support across desktop and mobile.

Here is a list of supported browsers:

  • Google Chrome supports it from version 38.
  • Mozilla Firefox supports it from version 25.
  • Microsoft Edge supports it from version 12.
  • Apple Safari supports it from version 8.
  • Opera includes support from version 25.
  • Internet Explorer does not support Math.trunc().
  • Mobile browsers like Chrome for Android and Safari on iOS support it.

If you work in older environments like IE, you may need a fallback.

Wrapping Up

In this article, you learned how JavaScript Math.trunc() works. You also saw how it handles decimals, negatives, and special edge values.

Here is a quick recap:

  • Math.trunc() removes the decimal part of a number.
  • It does not round. It just chops.
  • The function gives back only the whole number.
  • It keeps the sign of the number.
  • Math.trunc(7.9) gives 7. Math.trunc(7.1) also gives 7.
  • Math.trunc(-4.9) gives -4. Math.trunc(-0.9) gives 0.
  • Use it when you want no rounding — just straight-up cutting.
  • All modern browsers support it, except Internet Explorer.

What does Math.trunc() do with decimal numbers?

Math.trunc() removes the decimal part of a number and keeps only the whole integer. It does not round the number; it simply cuts off everything after the decimal point.

How does Math.trunc() handle negative numbers?

Math.trunc() keeps the sign of the number but removes the decimal part. For example, Math.trunc(-4.9) returns -4. It does not round the number up or down, just removes the decimal.

Is Math.trunc() supported in all browsers?

Math.trunc() is supported in most modern browsers like Chrome (v38+), Firefox (v25+), Edge (v12+), Safari (v8+), and Opera (v25+). However, it is not supported in Internet Explorer, so a fallback may be needed for that browser.

Similar Reads

JavaScript for…in Loop: Iterating Over Objects

You may need to loop through properties when you work with objects in JavaScript. The for...in loop helps you do…

JavaScript Hello World: Write a Simple Program in 3 Ways

If you are ready to take your first steps into the realm of web development, then there cannot be a…

JavaScript Syntax: The Complete Cheat Sheet

JavaScript syntax is what you will use when writing and reading javascript code, it is the foundation of using this…

Arrow Function in JavaScript: How it Works with Examples

Arrow function gives you short code and keeps this in the right scope in JavaScript. Understand the Arrow Function in…

JavaScript Math atan: Arc Tangent of a Number in Radians

Math.atan() function in JavaScript helps you to solve math problems in your code. It turns a number into an angle.…

JavaScript toSpliced Function Explained with Examples

The toSpliced function creates a new array without changing the original array in JavaScript. It returns a copy with new…

Understanding Comments in JavaScript for Beginners

Comments in JavaScript help you explain code and prevent mixing. They guide anyone who reads or edits your script. Understand…

Code Structure: How to Structure JavaScript Code

You have to organize your JavaScript code structure. A clean layout helps you read and fix your code. Whether you…

JavaScript for of Loop: Syntax and Examples

The for...of loop appeared to solve the problem of complex loops over arrays and strings. It gives you a way…

Mocha JavaScript Test Framework: Guide and Examples

Mocha is a JavaScript test framework for Node.js. It runs tests in sequence and shows results. It works with any…

Previous Article

Git Pull Rebase: How It Works, and When to Use It

Next Article

JavaScript Math floor: How It Works with Examples

Write a Comment

Leave a Comment

Your email address will not be published. Required fields are marked *


Subscribe to Get Updates

Get the latest updates on Coding, Database, and Algorithms straight to your inbox.
No spam. Unsubscribe anytime.