C Math trunc() Function

Beginner
⏱️ 8 min read
📚 Updated: Jul 2026
🎯 5 Examples
<math.h>

What You’ll Learn

The trunc() function removes the fractional part of a floating-point number by rounding toward zero. It keeps the integer portion without rounding up or down to the nearest whole number.

01

Truncate

Drop frac.

02

math.h

Link -lm.

03

Toward 0

Not nearest.

04

Negatives

-2.3 → -2.

05

vs floor

On negatives.

06

double out

Cast to int.

Definition and Usage

Truncation means discarding everything after the decimal point by moving toward zero on the number line. trunc(3.75) becomes 3.0; trunc(-2.3) becomes -2.0—not −3.

This is different from round(), which picks the nearest integer, and from floor(), which moves toward negative infinity. Use trunc when you literally want to chop off decimals without changing the sign’s magnitude beyond that.

💡
Beginner Tip

Need both whole and fractional parts? modf(x, &whole) splits a number in one call. trunc gives only the integral part toward zero—use it for display, grid snapping, or integer-only logic.

📝 Syntax

Standard C declaration:

C
double trunc(double x);

Related variants

C
float truncf(float x);           /* <math.h> */
long double truncl(long double x); /* <math.h> */

Parameters

  • x — any floating-point value.

Return Value

  • Integral part of x toward zero, returned as double.
  • trunc(3.75) returns 3.0; trunc(-2.3) returns -2.0.
  • If x is already an integer, NaN, or infinity, the result equals x.

Headers and linking

  • #include <math.h>
  • Compile: gcc trunc.c -std=c11 -o trunc -lm

⚡ Quick Reference

CallMeaningResult
trunc(3.75)Positive fraction3.0
trunc(-2.3)Toward zero-2.0
trunc(5.0)Already integer5.0
trunc(2.5)Not rounded up2.0
floor(-2.3)vs trunc (neg.)-3.0
Truncate
trunc(x)

Toward zero

Floor
floor(x)

Toward −∞

Round
round(x)

Nearest int

Split
modf(x, &w)

Whole + frac

Examples Gallery

Compile every example with gcc file.c -std=c11 -o out -lm. Truncation is common in display formatting, game coordinates, and splitting dollars from cents.

📚 Getting Started

Truncate a positive decimal to its integral part.

Example 1 — Truncate a Positive Decimal

Classic example from the reference: trunc(3.75).

C
#include <math.h>
#include <stdio.h>

int main(void) {
    double original = 3.75;
    double truncated = trunc(original);

    printf("Original number:  %.2f\n", original);
    printf("Truncated number: %.2f\n", truncated);

    return 0;
}

How It Works

The fractional 0.75 is discarded. The result is 3.0 as a double—not rounded up to 4.

Example 2 — trunc() vs floor() on Negatives

Truncation toward zero differs from floor toward negative infinity.

C
#include <math.h>
#include <stdio.h>

int main(void) {
    double x = -2.3;

    printf("x = %.1f\n", x);
    printf("trunc(x) = %.1f  (toward zero)\n", trunc(x));
    printf("floor(x) = %.1f  (toward -infinity)\n", floor(x));

    return 0;
}

How It Works

For negatives, trunc moves right toward zero (−2), while floor moves left (−3). This is the most common source of confusion.

📈 Practical Patterns

Whole numbers, rounding comparison, and splitting parts.

Example 3 — Already an Integer

When there is no fractional part, trunc returns the value unchanged.

C
#include <math.h>
#include <stdio.h>

int main(void) {
    double x = 5.0;

    printf("trunc(%.1f) = %.1f\n", x, trunc(x));

    return 0;
}

How It Works

No fractional part means nothing to remove. The same rule applies to -7.0 and other whole values.

Example 4 — floor, ceil, round, and trunc Compared

See how all four rounding functions behave on the same inputs.

C
#include <math.h>
#include <stdio.h>

int main(void) {
    double vals[] = { 2.3, 2.5, 2.8, -2.3, -2.5 };
    size_t i;

    printf("   x     floor  ceil  round  trunc\n");
    for (i = 0; i < 5; i++) {
        double x = vals[i];
        printf("%5.1f  %5.1f  %5.1f  %5.1f  %5.1f\n",
               x, floor(x), ceil(x), round(x), trunc(x));
    }

    return 0;
}

How It Works

On positives, trunc matches floor. On negatives and at 2.5, it diverges from round and floor. Pick the function that matches your rounding rule.

Example 5 — Split Dollars and Cents with modf()

Get the whole dollars via trunc and the leftover fraction via modf.

C
#include <math.h>
#include <stdio.h>

int main(void) {
    double price = 19.99;
    double whole;
    double fraction = modf(price, &whole);

    printf("Price:     $%.2f\n", price);
    printf("trunc():   $%.0f (whole dollars)\n", trunc(price));
    printf("modf whole:$%.0f, fraction: $%.2f\n", whole, fraction);

    return 0;
}

How It Works

trunc(19.99) is 19.0, not 20—truncation never rounds up. modf returns the fractional 0.99 and stores the integral part in whole. For currency, choose trunc vs round based on your business rules.

🚀 Common Use Cases

  • Display formatting — show integer part of a measurement without rounding up.
  • Game coordinates — snap to tile index by truncating pixel position.
  • Signal processing — extract integer cycles from a phase value.
  • Type conversion — safer than raw (int)x when you need explicit truncation semantics.
  • Pair with modf() — when you need both whole and fractional parts.

🧠 How trunc() Works

1

Receive double x

Any finite floating-point input.

Input
2

Drop fractional part

Move toward zero on the number line—no rounding to nearest.

Compute
3

Return as double

Integral value stored in floating-point format.

Output
=

Truncated value

Cast to int when you need a whole number for indexing.

📝 Notes

  • Rounds toward zero—not toward negative infinity like floor.
  • Returns double, not int—cast explicitly when needed.
  • trunc(-2.3) is −2; do not assume “floor behavior” on negatives.
  • Does not round to nearest—trunc(2.9) is 2, not 3.
  • NaN and infinity pass through unchanged.
  • Link with -lm when using GCC/Clang on Unix-like systems.

⚡ Optimization

trunc() is implemented in the platform math library. For positive values in a known-safe range, some code uses (int)x as a fast path—but that only matches trunc semantics for values that fit in int and when truncation toward zero is intended. Prefer trunc for clarity and correct negative handling.

Conclusion

trunc() removes the fractional part of a floating-point number by rounding toward zero. It matches intuitive “chop decimals” behavior for positives and differs from floor() on negatives.

You have now covered all core C math functions in this series. Browse the math function index or revisit floor() and round() to compare rounding strategies.

💡 Best Practices

✅ Do

  • Include <math.h> and link with -lm
  • Use trunc when you want to drop decimals toward zero
  • Compare with floor, ceil, and round for clarity
  • Use modf when you need the fractional part too
  • Cast to int after checking range

❌ Don’t

  • Assume trunc equals floor on negatives
  • Use trunc when nearest-integer rounding is needed
  • Forget the return type is double
  • Cast huge doubles to int without overflow checks
  • Confuse truncation with banker's rounding

Key Takeaways

Knowledge Unlocked

Five things to remember about trunc()

Truncate toward zero in C, explained simply.

5
Core concepts
📚 02

3.75→3

Positive.

Example
📈 03

-2.3→-2

Not -3.

Negatives
📄 04

vs round

Not nearest.

Compare
🔄 05

modf

Split parts.

Pair

❓ Frequently Asked Questions

trunc(x) removes the fractional part of x by rounding toward zero. For example, trunc(3.75) is 3.0 and trunc(-2.3) is -2.0. It does not round to the nearest integer.
Include <math.h> and link with -lm on many systems: gcc program.c -o program -lm. The function is declared as double trunc(double x).
For positive numbers they match. For negatives, trunc(-2.3) is -2 (toward zero) while floor(-2.3) is -3 (toward negative infinity). Pick trunc when you want to drop decimals without moving further from zero.
round(x) goes to the nearest integer (0.5 ties away from zero). trunc(x) always chops the fractional part toward zero. round(2.5) is 3.0; trunc(2.5) is 2.0.
No. trunc() returns double (e.g. 3.0). Cast when you need int: int n = (int)trunc(x); ensure the value fits in int range before casting.
truncf(x) takes float and returns float. truncl(x) takes long double and returns long double. Use the variant matching your variable type.
Did you know?

In C, casting a double to int with (int)x also truncates toward zero—but only when the value fits in int range. For clarity and large magnitudes, prefer trunc() or modf() from <math.h>.

Explore C Math Functions

You’ve reached the end of the math function series—browse the full index.

Math Functions Index →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

5 people found this page helpful