Why a float gets zero after Arduino integer division
Reproduce truncating integer division in an Arduino C++ sketch and fix the division before assigning its result.
The division happens before the assignment
When both operands are integers, 1 / 2 produces the integer 0. Assigning 0 to a float produces 0.00. The destination type does not change the arithmetic that happened earlier. Convert one operand before division, or use a floating-point literal.
Decide the range before choosing a type
Use a float for a fractional ratio, or keep a fixed-scale integer when a limited display precision is enough. Integer products on an Uno can overflow before assignment too. For a percentage, multiply only after choosing a suitable wide type and guard against a zero denominator. Serial.println with a second numeric argument prints the requested decimal places for floating-point values.
Open the exact Uno example
The sketch prints 0.00 followed by 0.50 at 9600 baud.
void setup() {
Serial.begin(9600);
const int part = 1, total = 2;
const float truncated = part / total;
const float ratio = static_cast<float>(part) / total;
Serial.println(truncated, 2);
Serial.println(ratio, 2);
}
void loop() {}Verified with [email protected] on Uno R3 (2026-12-05 UTC). This check did not use physical hardware.