Answer the question
In order to leave comments, you need to log in
How to increase the number of characters occupied by a number in C++?
How to make it so that, for example, the number 3 takes as many characters as the number 300?
Answer the question
In order to leave comments, you need to log in
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
cout << 300 << endl;
cout << setw(3) << 3 << endl;
cout << setfill('0') << setw(3) << 3 << endl;
return 0;
}
[[email protected] cpp]$ .iso++ t.cpp -o t
[[email protected] cpp]$ ./t
300
3
003
[[email protected] cpp]$
Example from here
TL;DR: printf("%03d") or printf("%3d")
/* printf example */
#include <stdio.h>
int main()
{
printf ("Characters: %c %c \n", 'a', 65);
printf ("Decimals: %d %ld\n", 1977, 650000L);
printf ("Preceding with blanks: %10d \n", 1977);
printf ("Preceding with zeros: %010d \n", 1977);
printf ("Some different radices: %d %x %o %#x %#o \n", 100, 100, 100, 100, 100);
printf ("floats: %4.2f %+.0e %E \n", 3.1416, 3.1416, 3.1416);
printf ("Width trick: %*d \n", 5, 10);
printf ("%s \n", "A string");
return 0;
}
Characters: a A
Decimals: 1977 650000
Preceding with blanks: 1977
Preceding with zeros: 0000001977
Some different radices: 100 64 144 0x64 0144
floats: 3.14 +3e+000 3.141600E+000
Width trick: 10
A string
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question