title: Formatting using DecimalFormat throws exception - "Cannot format given Object as a Number"
body: <p>This might look like a repeated question but I tried in all the below links and can't get a proper answer.</p>
  
  <p><a href="https://stackoverflow.com/questions/32174400/cannot-format-given-object-as-a-number-combobox">Cannot format given Object as a Number ComboBox</a></p>
  
  <p><a href="https://stackoverflow.com/questions/19780237/illegal-argument-exception">Illegal Argument Exception</a></p>
  
  <p>But I'm not getting what's wrong. Here is my code</p>
  
  <pre><code>DecimalFormat twoDForm = new DecimalFormat("#.##");
  double externalmark = 1.86;
  double internalmark = 4.0;
  System.out.println(String.valueOf((externalmark*3+internalmark*1)/4));
  String val = String.valueOf((externalmark*3+internalmark*1)/4);
  String wgpa1=twoDForm.format(val); // gives exception
  String wgpa2=twoDForm.format((externalmark*3+internalmark*1)/4)); // works fine
  System.out.println(wgpa1);
  </code></pre>
  
  <p>The <code>format</code> method takes Object type argument, so that's why I passed a String object which gives exception </p>
  
  <blockquote>
    <p><strong>Exception in thread "main" java.lang.IllegalArgumentException: Cannot</strong>
    <strong>format given Object as a Number.</strong></p>
  </blockquote>
  
  <p>But when I give double value as argument the program works fine. But if the method is defined with <code>Object</code> type argument why I'm getting an exception while passing a <code>String</code> and not getting exception while passing <code>double</code>?</p>