C llround() Function

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

Definition

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

You pass one double. The result is a long long. Include <math.h>. On GCC, link with -lm. Use lround() when a long is enough.

The halfway rule does not depend on the current floating-point rounding mode. That makes llround() predictable for money-style rounding when you always want .5 to move outward.

Syntax

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

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

Include <math.h>. On GCC, link with -lm. Print the result with %lld.

Library declarations
C
long long llround(double x);
long long llroundf(float x);
long long llroundl(long double x);

Parameters

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

Return Value

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

C Examples

Example 1 — Basic Rounding

Round positive and negative values, including a halfway case.

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

int main(void) {
    printf("%lld\n", llround(2.3));
    printf("%lld\n", llround(2.5));
    printf("%lld\n", llround(-2.5));
    return 0;
}

How It Works

  1. Include math.h. That declares llround.
  2. Below halfway. 2.3 becomes 2.
  3. Positive half. 2.5 moves away from zero to 3.
  4. Negative half. -2.5 moves away from zero to -3.

Example 2 — Round Money to Cents

Scale a price and round to the nearest cent as a whole number of cents.

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

int main(void) {
    double dollars = 12.345;
    long long cents = llround(dollars * 100.0);

    printf("%lld cents\n", cents);
    return 0;
}

How It Works

  1. Multiply by 100. Work in cents instead of dollars.
  2. Halfway rule helps. 12.345 * 100 is near 1234.5.
  3. llround outward. The half case becomes 1235.
  4. Store as long long. Useful for totals that can grow large.

Example 3 — Small Lookup Table

Print several inputs and their llround results side by side.

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

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

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

How It Works

  1. Loop over samples. Each value is rounded independently.
  2. Watch .5 cases. They always move away from zero.
  3. Non-half values. 2.49 is closer to 2.
  4. Good mental model. Use this table when you teach or debug rounding.

Frequently Asked Questions

llround() rounds a double to the nearest long long. Halfway values move away from zero.
x is required. It is the double to round.
A long long holding the rounded integer value.
Include <math.h>. On GCC, link with -lm.
llround() always uses half-away-from-zero. llrint() follows the current rounding mode.

Next: log()

Compute the natural logarithm of a positive double.

log() 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