Conversion
Lesson 63: Using int() Conversion
Learn how int() converts values to whole numbers, where decimals are removed, and how to use casting safely.
Progress indicator
Lesson 63 of 63
Learning Objectives
- Understand what int() conversion does in Arduino.
- Learn how decimal values become whole numbers.
- Differentiate int() casting from int variable declaration.
- Know when int() conversion is useful in embedded logic.
- Avoid common precision-loss mistakes with int().
Concept Explanation
What is int() Conversion
int() converts a value into an integer (whole number).
For decimal values, the fractional part is removed during conversion.
int() Syntax
int result = int(value);
int sensorLevel = int(analogAverage);How int() Works
- Evaluate source expression first.
- Take numeric result.
- Drop decimal fraction part.
- Store final whole number in int.
Type Casting Concept
Casting means explicitly forcing a value into another data type.
float x = 12.98;
int y = int(x); // y becomes 12int() vs int Variable
int value;declares an int variable type.int(expr)converts expression result to int.- Both can appear together in one line:
int v = int(expr);
When to Use int()
- When logic needs whole-number thresholds.
- When serial output should show rounded-down integer values.
- When converting float sensor values for integer-only APIs.
Example Code
This sketch converts float values using int() and compares whole-number results in logic.
const int LED_PIN = 2;
float temperatureC = 27.85;
float voltage = 3.92;
float sensorAverage = 512.67;
void setup() {
pinMode(LED_PIN, OUTPUT);
Serial.begin(115200);
}
void loop() {
int tempWhole = int(temperatureC);
int voltageWhole = int(voltage);
int avgWhole = int(sensorAverage);
int pwmLevel = int(voltage * 64.0);
if (tempWhole >= 28) {
digitalWrite(LED_PIN, HIGH);
} else {
digitalWrite(LED_PIN, LOW);
}
Serial.print("temperatureC=");
Serial.print(temperatureC, 2);
Serial.print(", int(temperatureC)=");
Serial.print(tempWhole);
Serial.print(", int(voltage)=");
Serial.print(voltageWhole);
Serial.print(", int(sensorAverage)=");
Serial.print(avgWhole);
Serial.print(", pwmLevel=");
Serial.println(pwmLevel);
delay(800);
}Example Code Explanation
int(temperatureC)removes decimal part and stores whole number.int(voltage)converts voltage to integer for simplified display.int(sensorAverage)converts averaged reading for integer checks.pwmLevelshows conversion after expression multiplication.- LED threshold uses integer value to decide ON/OFF state.
- Serial output compares original float values with converted int values.
What Happens Inside
- Expression computes in source type (often float).
- Cast converts final numeric value to int.
- Fractional part is discarded.
- Converted whole number is used for logic/output.
Common Mistakes with int()
- Assuming int() rounds values instead of truncating decimals.
- Converting too early and losing precision needed later.
- Using int cast in code that still needs decimal behavior.
Best Practices for int()
- Keep calculations in float until final conversion point.
- Document when truncation is intentional.
- Use Serial prints while learning conversion behavior.