title: Java float 123.129456 to 123.12 without rounding
body: <p>How do you cut down <strong>float</strong> primitive in java to <strong>two decimal</strong> places, without using rounding?:</p>
  <pre><code>123.99999 to 123.99
  -8.022222 to -8.02
  </code></pre>
  <p>There should be no rounding just cut of the decimal places and leave two.</p>
  <p>Second point is how do you <strong>validate</strong> or count how many decimals are after the point?:</p>
  <pre><code>123.99 will give true or 2
  123.999 will give false or 3
  </code></pre>
  <hr />
  <h2>UPDATE</h2>
  <p>The numbers are String input so I think I will go with this as suggested; and I'll just have int try/catch block for any exceptions. Any suggestions how to make this work any smarter way are welcome:</p>
  <pre><code>public static float onlyTwoDecimalPlaces(String number) {
      StringBuilder sbFloat = new StringBuilder(number);
      int start = sbFloat.indexOf(&quot;.&quot;);
      if (start &lt; 0) {
          return new Float(sbFloat.toString());
      }
      int end = start+3;
      if((end)&gt;(sbFloat.length()-1)) end = sbFloat.length();
  
      String twoPlaces = sbFloat.substring(start, end);
      sbFloat.replace(start, sbFloat.length(), twoPlaces);
      return new Float(sbFloat.toString());
  }
  </code></pre>