C lround() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
math.h

Definition

In C, lround() rounds a double to the nearest integer value and returns it as a long. Halfway cases move away from zero, so lround(1.5) is 2 and lround(-1.5) is -2.

You pass one double. The result is a long. Include <math.h>. On GCC, link with -lm. For a wider result, use llround().

The halfway rule is fixed. Changing the floating-point rounding mode does not change how lround() treats .5 values.

Syntax

Pass the floating-point number. Start with this call.

C
lround(1.5);
CallWhat it does
lround(1.5)Rounds halfway away from zero and returns 2 as a long.

Include <math.h>. On GCC, link with -lm. Print with %ld on most systems.

Library declarations
C
long lround(double x);
long lroundf(float x);
long lroundl(long double x);

Parameters

ParameterDescription
xRequired. The double to round to the nearest integer value.

Return Value

Return ValueDescription
longThe rounded integer as a long. Halfway values move away from zero.

C Examples

Example 1 — Basic Rounding

Round positive and negative values, including halfway cases.

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

int main(void) {
    printf("%ld\n", lround(3.4));
    printf("%ld\n", lround(3.5));
    printf("%ld\n", lround(-3.5));
    return 0;
}

How It Works

  1. Include math.h. That declares lround.
  2. Below halfway. 3.4 becomes 3.
  3. Positive half. 3.5 becomes 4.
  4. Negative half. -3.5 becomes -4.

Example 2 — Round Quiz Scores

Turn average scores into whole points stored in a long.

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

int main(void) {
    double avg = 87.5;
    long points = lround(avg);

    printf("points = %ld\n", points);
    return 0;
}

How It Works

  1. Start with average. The value may include a fractional part.
  2. Apply lround. Halfway moves away from zero.
  3. Store as long. Whole points are easier to print and compare.
  4. Explain the rule. Students often expect .5 to round up for positives.

Example 3 — Half-Case Table

List several inputs so the halfway rule is easy to see.

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

int main(void) {
    double vals[] = { -2.5, -1.25, 0.5, 1.25, 2.5 };
    int i;

    for (i = 0; i < 5; i++) {
        printf("%.2f -> %ld\n", vals[i], lround(vals[i]));
    }
    return 0;
}

How It Works

  1. Loop the samples. Each call is independent.
  2. Watch ±.5. They move away from zero.
  3. Non-half values. Closest integer wins as usual.
  4. Teach with output. A table beats a long paragraph.

Frequently Asked Questions

lround() rounds a double to the nearest long. Halfway values move away from zero.
x is required. It is the double to round.
A long holding the rounded integer value.
Include <math.h>. On GCC, link with -lm.
Both use the same halfway idea, but lround() returns long while round() returns double.

Next: modf()

Split a floating-point value into integer and fractional parts.

modf() tutorial →

CODETOFUN NEWS

Stay Ahead in Tech

Get the latest IT news, programming updates, and technology trends directly on your phone.

Your daily dose of tech, straight to your feed.

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.

9 people found this page helpful