adty4
adty4

Reputation: 29

Right aligning digits in c?

I have a code snippet that looks like the following -

int main() {
  double firstNumber = 245.3252;
  double secondNumber = 32.4324;
  printf("This is my first number: %.2f", firstNumber);
  printf("And what's gonna be in the front if my second number: %.2f", secondNumber);
  return 0;
}

The problem with such code is it will give me an output which looks like the following -

This is my first number: 245.32
And what's gonna be in the front if my second number: 32.43

However I want the output to look like this (Expected Output)-

This is my first number:                             245.32
And what's gonna be in the front if my second number: 32.43

I was trying to use width modifiers like %20s or so but then if my first number is a ten's place number, then the alignment goes off again. Can someone please give me an idea on how I can make them look perfectly right aligned? Thanks!

Upvotes: 1

Views: 11557

Answers (5)

Antonio Marcos
Antonio Marcos

Reputation: 31

Well, to your particular issue, try this:

printf("This is my first number: \t\t\t %.2f \n", firstNumber); printf("And what's gonna be in the front if my second number: %.2f", secondNumber);

\t\t\t ( 3 times ) + 4 blank spaces before %.2f

Upvotes: 0

chux
chux

Reputation: 154065

Format the leading text

int main(void) {
  double Number[] = { 245.3252, 32.4324 };
  const char *text[] = {"This is my first number:", 
                         "And what's gonna be in the front if my second number:"};

  // Find or set max width of text
  int width = max(strlen(text[0]), strlen(text[1]));

  // Calculate or set max width of numbers
  int maxnumwidth = 6;

  int i;
  for (int i= 0; i < 2; i++) {
    printf("%-*s %*.2f\n", width, text[i], maxnumwidth, Number[i]);
  }

  return 0;
}

%-*s:
- left justify
* Get width from arguments.
s String

%*.2f:
* Get width from arguments.
.2 print to 2 decimal places.
f float or double

Upvotes: 2

sh4nkc
sh4nkc

Reputation: 358

You can insert spaces.

Let's call the length of the longest string m and the other n. In this example, longest string is "And what's gonna be in the front if my second number: 32.43".

Now, the string with the longest length won't require any modifications. But, the other string will need m - n spaces added after the sentence but before the number. So, after "This is my first number:" but before "245.32".

Note: In fixed-width font, this would be aligned. But, in certain fonts the alignment won't work properly using this method because the width of the characters are not fixed.

Upvotes: 0

R Sahu
R Sahu

Reputation: 206697

Simplest method: add the necessary space characters to the format string in the first statement while making sure that you also fix the number of characters used by the number.

int main() {
  double firstNumber = 245.3252;
  double secondNumber = 32.4324;
  printf("This is my first number:                              %6.2f", firstNumber);
  printf("And what's gonna be in the front if my second number: %6.2f", secondNumber);
  return 0;
}

Upvotes: 2

Alexis King
Alexis King

Reputation: 43872

Right justification is actually the default, you just need to ensure you specify the width properly. You want something like this:

printf("This is my first number:                              %8.2f", firstNumber);
printf("And what's gonna be in the front if my second number: %8.2f", secondNumber);

Upvotes: 2

Related Questions