Numbers and Literals

Rounding Floating Point Numbers

How do I round floating point numbers to the nearest integer? For example, if I want 2.7 to become 3?

3D art showing a character using an abacus

There are several ways to round floating-point numbers in C++, each with its own specific behavior. Let's explore the main approaches:

Using Standard Library Functions

The <cmath> library provides several useful rounding functions:

#include <cmath>
#include <iostream>
using namespace std;

int main() {
  float damage{24.7f};

  // Round to nearest integer
  int roundedDamage{
    static_cast<int>(std::round(damage))};

  // Round down (floor)
  int floorDamage{
    static_cast<int>(std::floor(damage))};

  // Round up (ceiling)
  int ceilingDamage{
    static_cast<int>(std::ceil(damage))};

  cout << "Original: " << damage << "\n";
  cout << "Rounded: " << roundedDamage << "\n";
  cout << "Floor: " << floorDamage << "\n";
  cout << "Ceiling: " << ceilingDamage;
}
Original: 24.7
Rounded: 25
Floor: 24
Ceiling: 25

Rounding to Decimal Places

Sometimes you want to round to a specific number of decimal places:

#include <cmath>
#include <iostream>
using namespace std;

float roundToDecimalPlaces(
  float value, int decimalPlaces
) {
  double multiplier{pow(10.0f, decimalPlaces)};
  return round(
    value * multiplier) / multiplier;
}

int main() {
  float health{95.6789f};

  // Round to various decimal places
  float oneDecimal{
    roundToDecimalPlaces(health, 1)};
  float twoDecimals{
    roundToDecimalPlaces(health, 2)};

  cout << "Original: " << health << "\n";
  cout << "One decimal place: "
    << oneDecimal << "\n";
  cout << "Two decimal places: "
    << twoDecimals;
}
Original: 95.6789
One decimal place: 95.7
Two decimal places: 95.68

Practical Example: Damage Calculations

Here's a more practical example showing how rounding can be used in game mechanics:

#include <cmath>
#include <iostream>
using namespace std;

float calculateDamage(
  float baseDamage, float multiplier
) {
  float rawDamage{baseDamage * multiplier};

  // Round to nearest whole number
  return round(rawDamage);
}

int main() {
  float weaponDamage{10.0f};
  float criticalMultiplier{1.5f};

  float damage{
    calculateDamage(
      weaponDamage, criticalMultiplier)};
  cout << "Critical hit damage: "
    << damage << "\n";

  // Multiple calculations
  float multipliers[]{
    0.8f, 1.0f, 1.2f, 1.5f
  };
  cout <<
    "\nDamage with different multipliers:";
  for (float mult : multipliers) {
    cout << "\nMultiplier " << mult << ": "
      << calculateDamage(
        weaponDamage, mult);
  }
}
Critical hit damage: 15

Damage with different multipliers:
Multiplier 0.8: 8
Multiplier 1: 10
Multiplier 1.2: 12
Multiplier 1.5: 15

Answers to questions are automatically generated and may not have been reviewed.

3D art showing a progammer setting up a development environment
Part of the course:

Intro to C++ Programming

Become a software engineer with C++. Starting from the basics, we guide you step by step along the way

Free, unlimited access

This course includes:

  • 60 Lessons
  • Over 200 Quiz Questions
  • 95% Positive Reviews
  • Regularly Updated
  • Help and FAQ
Free, Unlimited Access

Professional C++

Comprehensive course covering advanced concepts, and how to use them on large-scale projects.

Screenshot from Warhammer: Total War
Screenshot from Tomb Raider
Screenshot from Jedi: Fallen Order
Contact|Privacy Policy|Terms of Use
Copyright © 2024 - All Rights Reserved