MMGameslogo  MMGames
TwitterSharebutton  FacebookSharebutton   
learn through sufferingC Language
learn through sufferingC Language

Number formatting

Integer alignment
We've used the printf function frequently until now, but there's still functionality that hasn't been explained.
The `printf` function allows you to specify the number of digits to be displayed for characters or numbers.

The following program displays the output as is, without specifying the number of digits as before.

Source code
#include <stdio.h>

int main(void)
{
    int a = 10000, b = 500, c = 3;

    printf("Aは %d is.\n", a);
    printf("Bは %d is.\n", b);
    printf("Cは %d is.\n", c);

    return 0;
}

The output of this program is as follows:

Execution results
Aは 10000 is.
Bは 500 is.
Cは 3 is.

As you can see from the results, the numbers are displayed as they are.
It's misaligned with the other numbers.
Displaying several numbers with many digits becomes very difficult to read.

To improve readability of the results, please pad the beginning of numbers with spaces and align them at the end.
To right-align numbers when using the printf function, you can do the following.

Align numbers by their trailing ends.
"%d digits"

Placing a number between the output conversion specifier and the variable will pad the output with spaces to match that number of digits.
The following program is an example formatted with 5 digits for readability.

Source code
#include <stdio.h>

int main(void)
{
    int a = 10000, b = 500, c = 3;

    printf("Aは %5d is.¥n", a);
    printf("Bは %5d is.¥n", b);
    printf("Cは %5d is.¥n", c);

    return 0;
}

The output of this program is as follows:

Execution results
Aは 10000 is.
Bは  500 is.
Cは   3 is.

As you can see from the result, spaces have been added to improve readability based on the number of digits.
If the number of digits in the number exceeds the specified number of digits, it will be adjusted to match the number of digits in the number.

Therefore, if you specify the expected maximum number of digits, the numbers will be aligned and displayed accordingly.
Also, when displaying negative values, the minus sign is treated as a single digit.
"If there's a possibility of displaying negative values, specify one digit larger."
Computational representation
The printf function allows for the display of numbers padded with zeros, a common feature in computing.
Leading zeros will cause numbers to be displayed with leading zeros instead of spaces.
The following program is an example modified to pad with leading zeros.

Source code
#include <stdio.h>

int main(void)
{
    int a = 10000, b = 500, c = 3;

    printf("Aは %05d is.\n", a);
    printf("Bは %05d is.\n", b);
    printf("Cは %05d is.\n", c);

    return 0;
}

The output of this program is as follows:

Execution results
Aは 10000 is.
Bは 00500 is.
Cは 00003 is.

I feel like the display has a sort of computer-like quality.
Decimal alignment of real numbers
"For displaying real numbers, you can specify both the total number of digits and the number of digits after the decimal point."

Numeric alignment (for real numbers)
"%total_digits.decimal_digitsf"

In the previous section, we explained how to declare variables using the following notation.
"For example, in %6.2f, the integer part is interpreted as having 3 digits, the decimal point takes 1 digit, and the fractional part has 2 digits, resulting in a total of 6 digits."

The following program demonstrates how to format real numbers.

Source code
#include <stdio.h>

int main(void)
{
    double pi = 3.14159;
    printf("%6.2f\n",pi);
    printf("123456\n");
    
    return 0;
}

The output of this program is as follows:

Execution results
3.14
123456

The following line is included for readability of the digits.
It appears the display is six digits total, with one digit for the decimal point and two digits for the fractional part.
If you set the number of decimal places to 0, the decimal point will not be displayed.

You can also specify other options.
詳しくは Words and symbols のOutput format specifiersを参照してください。


About This Site

Learning C language through suffering (Kushi C) is
This is the definitive introduction to the C language.
It systematically explains the basic functions of the C language.
The quality is equal to or higher than commercially available books.

Part 0: Program Overview
  1. What is a program?
Chapter 3: Displaying on the Screen
  1. String Display
  2. newline character
  3. Practice Problem 3
Chapter 4: Displaying and Calculating Numbers
  1. Display of numbers
  2. Basic calculations
  3. Numeric types
  4. Practice Problem 4
Chapter 6: Input from the Keyboard
  1. input function
  2. The fear of input
  3. Practice Problem 6
Chapter 9: Repeating a Fixed Number of Times
  1. Iterative sentence
  2. How Loops Work
  3. Practice Problem 9
Chapter 10: Repeating Without Knowing the Number of Times
  1. Unspecified loop
  2. Input validation
  3. Practice Problem 10
Chapter 13: Handling Multiple Variables at Once
  1. Handling multiple variables collectively.
  2. Arrays
  3. Practice Problem 13
Chapter 19: Dynamic Arrays
  1. Create arrays freely.
  2. Practice Problem 19