Fruitytart
Fruitytart

Reputation: 41

How can I make the numbers that I print in this code right adjusted?

How can I make the numbers that I print in this code right adjusted? Since the numbers in the code are variables do I need to do something different than normally adjusting it?

 a = 4                                                                                                      ;
 b = 4                                                                                                      ;
 c = 1                                                                                                      ;
 x = 2                                                                                                      ;
 Root                      = (-1*b + Math.sqrt( Math.pow(b,2) - 4*a*c)/ 2*a                                );
 CoefficientOfXSquared     = (-(b*x+c)/(Math.pow(x,2))                                                     );
 CoefficientOfX            = (-(a*x+c)-c/x                                                                 );
 Constant                  = (-(a*Math.pow(x,2)+b*x)                                                       );
 System.out.println("\n\n\t    Given that: \n\t    CoefficientOfXSquared = " +a                            );
 System.out.println("\n\t    CoefficientOfX        = " +b                                                  );
 System.out.println("\n\t    Constant              = " +c                                                  );
 System.out.println("\n\t    Root                  = " +x                                                  );
 System.out.println("\n\n\t    x = " + Root                                                                );
 System.out.println("\n\t    a = " +  CoefficientOfXSquared                                                );
 System.out.println("\n\t    b = " +  CoefficientOfX                                                       );
 System.out.println("\n\t    c = " +  Constant                                                             );
 System.out.println("\n\n\n"                                                                               );

I would appreciate it if someone could explain how to make it right adjusted.

Upvotes: 1

Views: 111

Answers (3)

cliff2310
cliff2310

Reputation: 590

Try using string format method to print them. Like this:

double x=1234.56;
double y=78.678;
System.out.printf("%n\t Root          =  %12.4f",  x);
System.out.printf("%n\t Constant      =  %12.4f%n",  y);

which gives:

Root          =     1234.5600
Constant      =       78.6780

I am assuming that these are doubles. Look up printf for more info. Cliff

Upvotes: 1

sperumal
sperumal

Reputation: 1519

You can do this to right justify the text.

String.format("%50s", "Root = " + root);

Try String format function, format arguments provide many options to customize the printing.

Upvotes: 1

David Kroukamp
David Kroukamp

Reputation: 36423

I think the docs are about the best for this, shows how to format numerics outputs using printf() here is also a great tutorial on formatting numbers in java

Upvotes: 1

Related Questions