title: The maximum digit width on different font and font size
body: <p>I need to get the maximum width of 1,2,3,....8,9 on different font and font size.</p>
  
  <p>In c#, the open source code is:</p>
  
  <pre><code>    protected double MaxDigitWidth(string fontName, double dblSizePt)
      {
          double dblMaxDigitWidth = 0.0;
  
          // Excel does not use the specified font size, 
          // instead, font size is rounded to an integral pixel size
          // check if this works on non-Windows platforms, otherwise 96dpi could do fine here
        //  Graphics g = System.Drawing.Graphics.FromHwnd(new IntPtr());
          float fSizePxl = (float)Math.Round(96 * dblSizePt / 72);
          float fSizePt = (72 * fSizePxl / 96);
          FontStyle fontStyle;
          if (fontName == "Monotype Corsiva")
              fontStyle = FontStyle.Italic;
          else
              fontStyle = FontStyle.Regular;
  
          Font font = new Font(fontName, fSizePt, fontStyle, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
          for (int i = 0; i &lt; 10; i++)
          {
              // use a Label on a .NET 2.0 Form to measure the width of a digit
              Form f = new Form();
              Label l = new Label();
              l.UseCompatibleTextRendering = false;
              l.Font = font;
              l.AutoSize = true;
              f.Controls.Add(l);
              l.Text = Convert.ToString(i) + Convert.ToString(i);
              f.ResumeLayout(false);
              f.PerformLayout();
              int iWidth2 = l.Width;
  
              l.Text = Convert.ToString(i);
              f.PerformLayout();
  
              // we measure twice so we can remove the padding
              int iWidth1 = l.Width;
  
              if (iWidth2 - iWidth1 &gt; dblMaxDigitWidth)
              {
                  dblMaxDigitWidth = iWidth2 - iWidth1;
              }
          }
          return dblMaxDigitWidth;
  
      }
  </code></pre>
  
  <p>I implemented it in Java. But the results are different.</p>
  
  <pre><code>protected double MaxDigitWidth(String fontName, double dblSizePt)
  {
      double dblMaxDigitWidth = 0.0;
      Font f;
      if (fontName == "Monotype Corsiva")
          f = new Font(fontName, Font.ITALIC ,(int)dblSizePt);
      else
          f = new Font(fontName, Font.PLAIN ,(int)dblSizePt);
      JComponent t = new JLabel();
      FontMetrics fm = t.getFontMetrics(f);
      for (int i = 0; i &lt; 10; i++)
      {       
       int iWidth2 = fm.stringWidth(String.valueOf(i)+String.valueOf(i));
       int iWidth1 = fm.stringWidth(String.valueOf(i));
  
       if (iWidth2 - iWidth1 &gt; dblMaxDigitWidth)
       {
           dblMaxDigitWidth = iWidth2 - iWidth1;
       }
      }
       return dblMaxDigitWidth;
  
  }
  </code></pre>
  
  <p>But the result is different betweent c# and java.(I run them on the same machine.)</p>
  
  <ol>
  <li><p>the result of c#</p>
  
  <pre><code>      Font and Font size                Maximum digit widht 
      a = MaxDigitWidth("Arial", 11.0);    8
      a = MaxDigitWidth("Arial", 12.0);    9 
      a = MaxDigitWidth("Arial", 13.0);    9 
      a = MaxDigitWidth("Arial", 14.0);    11 
      a = MaxDigitWidth("Arial", 15.0);    11 
      a = MaxDigitWidth("Arial", 16.0);    12 
      a = MaxDigitWidth("Arial", 17.0);    13 
      a = MaxDigitWidth("Arial", 18.0);    13   
      a = MaxDigitWidth("Arial", 19.0);    14   
      a = MaxDigitWidth("Arial", 20.0);    15   
  </code></pre></li>
  <li><p>the result of java</p>
  
  <pre><code>    Arial-11           6.0
      Arial-12           7.0
      Arial-13           7.0
      Arial-14           8.0
      Arial-15           8.0
      Arial-16           9.0
      Arial-17           9.0
      Arial-18           10.0
      Arial-19           11.0
      Arial-20           11.0
  </code></pre></li>
  </ol>