HOWTO · Arduino

How to Round a Float to the Nearest Integer in Arduino

Learn how Arduino round() rounds float values to the nearest integer, how positive and negative halfway values behave, and how to round to a chosen number of decimal places.

On this page

Use Arduino’s round() function to get the nearest integer value from a float. Values halfway between integers round away from zero, so 5.5 becomes 6 and -3.5 becomes -4. The result can be stored as an int when a whole-number value is useful.

What round() Does in Arduino

round() returns a floating-point value whose value is the nearest integer. Assigning that result to an int stores the integer value. For example, round(3.6) produces 4, while round(-2.3) produces -2. The function changes the value used by later calculations; it does not change the original variable unless you assign the result back to it.

This complete C++ program illustrates integer rounding and scaling a measurement to two decimal places. It uses the standard C++ round function, which follows the same rule; in an Arduino sketch, call round() and send values to Serial as needed. The example is self-contained and prints labeled results when run.

#include <cmath>
#include <cstdio>

int main() {
    float positive = 5.5f;
    float belowZero = -3.5f;
    float measurement = 7.4567f;

    int roundedPositive = static_cast<int>(std::round(positive));
    int roundedBelowZero = static_cast<int>(std::round(belowZero));
    float roundedMeasurement = std::round(measurement * 100.0f) / 100.0f;

    std::printf("positive: %d\n", roundedPositive);
    std::printf("below zero: %d\n", roundedBelowZero);
    std::printf("measurement: %.2f\n", roundedMeasurement);
    return 0;
}

Round Positive and Negative Values

For positive values, the nearest integer is selected: values below a halfway point round down, and values at or above it round up. Thus, 5.4 becomes 5, while 5.5 and 5.6 become 6. At an exact halfway point, the result moves away from zero.

For negative values, the same halfway rule applies. For example, -3.4 becomes -3, while -3.5 and -3.6 become -4. This is not bankers’ rounding: halfway values do not switch direction to favor an even integer. If the result will be assigned to an int, choose input values whose rounded result fits the range of that type.

Round to a Chosen Number of Decimal Places

round() by itself rounds to an integer. To round a value to a chosen number of decimal places, multiply by the corresponding power of ten, call round(), then divide by that factor. For two decimal places, the factor is 100; for example, 7.4567 becomes approximately 7.46.

This scaling method works for many display and calculation needs, but float values are stored with finite binary precision. A decimal value may not be represented exactly, so a value close to a halfway point can behave differently from ideal decimal arithmetic. Keep the original value for calculations that need more precision, and format the value for display when you only need a fixed number of visible decimal places. Scaling also does not make the stored value exact; it changes the rounded result according to the available precision.

Use Rounded Values in an Arduino Sketch

In a sketch, assign the result where an integer is useful, such as when choosing an array index or displaying a whole-number reading. Check that the rounded value fits the range of int before assigning it. For a serial display, Serial.println(round(sensorValue)); prints the rounded value. The complete program above is a host-side C++ illustration and does not use Arduino hardware or the Arduino Serial object.

Rounding is optional before mapping: map() accepts integer arguments, so rounding may help when converting a floating-point reading to an integer first, but it is not required for every mapping task. See the Arduino map() function for its integer mapping behavior.