title: formatting in java (Russian Peasant Multiplication)
body: <p>I want to format the russian peasant multiplication with this scheme: The problem is that its not working with bigger numbers.</p>
  <pre><code>47 * 42
  -------
  23   84
  11  168
   5  336
   2  672
   1 1344
  =======
     1974
  </code></pre>
  <p>But when I have bigger numbers my format does not match:</p>
  <pre><code>470 * 420
  ---------
  235  840
  117 1680
  58 3360
  29 6720
  14 13440
   7 26880
   3 53760
   1 107520
  =========
     197400
  </code></pre>
  <p>It should come out like this:</p>
  <pre><code>470 * 420
  ----------
  235    840
  117   1680
   58   3360
   29   6720
   14  13440
    7  26880
    3  53760
    1 107520
  ==========
     197400
  </code></pre>
  <p>Is their a way to format it for variable numbers? I currently have it hard coded with %2d and %4d?</p>
  <p>This is my current code:</p>
  <pre><code>public class Main {
  
  public static void main (String[] args) {
      int num1=47;
      int num2=42;
      int product=0;
      String testNum1 = &quot;&quot;;
      String testNum2 = &quot;&quot;;
  
  
      if(num1%2!=0)
          product=product+num2;
  
      System.out.println(num1 + &quot; * &quot;+num2);
      int printLength = String.valueOf(num1).length() + String.valueOf(num2).length() + 3;
      for (int i = 0; i &lt; printLength; i++){
          System.out.print('-');
      }
      System.out.println();
      while(num1!=1)
      {
          num1=num1/2;
          num2=num2*2;
  
          int x = String.valueOf(num1).length();
  
          if(num1%2!=0)
              product=product+num2;
  
          testNum1 = String.format(&quot;%2d&quot;, num1);
          testNum2 = String.format(&quot;%4d&quot;, num2);
          System.out.println(testNum1 + &quot; &quot; + testNum2);
      }
      for (int i = 0; i&lt;printLength; i++){
          System.out.print(&quot;=&quot;);
      }
      System.out.println();
      System.out.print(&quot;   &quot;);
      System.out.print(product);
  }
  </code></pre>
  <p>}</p>