title: How to compare percentages that are strings?
body: <p>I have a string <code>"+1.29%"</code> or <code>"-1.29%"</code></p>
  
  <p>How can I convert this into a float or double so I can compare the values?</p>
  
  <p>This is how my comparator class looks like:</p>
  
  <pre><code> package org.stocktwits.helper;
  
  import java.util.Comparator;
  
  import org.stocktwits.model.Quote;
  
      public class PercentChangeComparator implements Comparator&lt;Quote&gt;
      {
          public int compare(Quote o1, Quote o2) {
              try{
                  String subChange1 = o1.getPercentChange();
                  String subChange2 = o2.getPercentChange();
  
                  float change1 = Float.valueOf(subChange1.substring(0, subChange1.length() - 1));
                  float change2 = Float.valueOf(subChange2.substring(0, subChange2.length() - 1));
  
                  if (change1 &lt; change2) return -1;
                  if (change1 == change2) return 0; // Fails on NaN however, not sure what you want
                  if (change2 &gt; change2) return 1;
  
                  return 0;
              }catch(NullPointerException e){ System.out.println(e); return 0;}
              catch(NumberFormatException e){ System.out.println(e); return 0;}
          }
      }
  </code></pre>