Controlling Decimal Places

When I print floating point numbers, they sometimes show way more decimal places than I want. How can I control this?

You can control how floating point numbers are displayed using various formatting options in C++. Here are some common approaches:

Using precision()

The simplest way is to set how many decimal places to show using precision():

#include <iostream>
using namespace std;

int main() {
  float Pi{3.14159265359};

  // Default output
  cout << "Default Pi: " << Pi << '\n';

  // Set to 2 decimal places
  cout.precision(2);
  cout << "Pi with 2 digits: " << Pi << '\n';

  // Set to 4 decimal places
  cout.precision(4);
  cout << "Pi with 4 digits: " << Pi;
}
Default Pi: 3.14159
Pi with 2 digits: 3.1
Pi with 4 digits: 3.142

Using fixed

Sometimes we want to always show a specific number of decimal places, even if they're zeros. We can use fixed for this:

#include <iostream>
using namespace std;

int main() {
  float Price{19.99};
  float WholeNumber{20.0};

  // Set to show exactly 2 decimal places
  cout.precision(2);
  cout << fixed;

  cout << "Price: $" << Price;
  cout << "\nWhole number: $" << WholeNumber;

  // You can turn off fixed mode if needed
  cout.unsetf(ios::fixed);
  cout << "\nBack to default: " << Price;
}
Price: $19.99
Whole number: $20.00
Back to default: 20

Using showpoint

If you want to always show the decimal point, even for whole numbers, use showpoint:

#include <iostream>
using namespace std;

int main() {
  float Score{100.0};

  // Default - no decimal for whole numbers
  cout << "Score: " << Score << '\n';

  // Force showing decimal point
  cout << showpoint;
  cout << "Score with decimal: " << Score;
}
Score: 100
Score with decimal: 100.000

Remember:

  • These settings stay in effect until you change them
  • Different settings might make sense for different situations (like showing exactly 2 decimal places for money)
  • Be consistent in how you display numbers in your program
  • When working with money, remember that floating point numbers can have small rounding errors, so you might want to store cents as integers instead

Types and Literals

Explore how C++ programs store and manage numbers in computer memory, including integer and floating-point types, memory allocation, and overflow handling.

Questions & Answers

Answers are generated by AI models and may not have been reviewed. Be mindful when running any code on your device.

Memory Usage in Modern Computers
Why do we need to care about memory usage in modern computers when they have so much RAM available?
How Computers Store Text
In the Memory section, you mention bits can be used to represent characters. How can 0s and 1s represent letters?
Monitoring Program Memory Usage
How can I know how much memory my entire program is using? Is there a way to check this?
When to Use Small Integer Types
In the real world, when would I choose to use a smaller integer type like int8_t instead of just using regular int?
Uses for Unsigned Integers
If unsigned integers are problematic, why do they exist at all? Are there any real-world cases where they're useful?
Preventing Number Overflow
How can I check if my number will cause an overflow before performing a calculation?
Or Ask your Own Question
Get an immediate answer to your specific question using our AI assistant