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 Math log: How it Works with Examples

JavaScript Math.log() was added to help people find the natural log of a number. It solves real problems in science…

How Does JavaScript Work to Run Code in the Web Browser

Click a button on a website. Something changes. A menu opens or a message pops up. That’s how JavaScript works.…

JavaScript Math ceil: Round Numbers with Precision

Some numbers do not stay whole. You get decimal parts when you divide or calculate prices. You may need to…

JavaScript Math acos Function

The Math.acos function finds the angle in radians from a cosine value in JavaScript. It gives a value between 0…

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…

forEach Loop in JavaScript: A Complete Guide with Examples

The forEach loop in JavaScript allows you to go through each item in an array. You do not have to…

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.…

How to Use the Ternary Operator in JavaScript

The ternary operator in JavaScript keeps your code short. It helps you write conditions inside one line. JavaScript lets you…

JavaScript Hoisting: How It Works with Examples

You will face hoisting early when you write JavaScript. It affects how your code runs, and it may confuse you…

JavaScript Loops and Iteration: How They Work with Examples

You use loops in JavaScript to repeat code without copying it many times. This helps when you handle lots of…

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.