title: Rounding Currency to Nearest Dollar, how did you do it?
body: <p>The following piece of code works on all phones, in my production app, except for the Galaxy Nexus running 4.1.2. The Play store sent me the error, I will first post the code, and then the error:</p>
  
  <pre><code>    // Roughly formats the currency by loping off the decimal places (amount
  // comes already rounded)
  public static String formatCurrencyRound(double amount) {
      String currency = formatCurrency(amount); // Returns "$8.00"
  
      return currency.substring(0, currency.indexOf("."));
  }
  </code></pre>
  
  <p>The Error:</p>
  
  <pre><code>java.lang.RuntimeException: Unable to resume activity {com.ootpapps.saving.made.simple/com.ootpapps.saving.made.simple.SavingsPlanOverview}: java.lang.StringIndexOutOfBoundsException: length=9; regionStart=0; regionLength=-1
  at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2575)
  at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:2603)
  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2089)
  at android.app.ActivityThread.access$600(ActivityThread.java:130)
  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
  at android.os.Handler.dispatchMessage(Handler.java:99)
  at android.os.Looper.loop(Looper.java:137)
  at android.app.ActivityThread.main(ActivityThread.java:4745)
  at java.lang.reflect.Method.invokeNative(Native Method)
  at java.lang.reflect.Method.invoke(Method.java:511)
  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
  at dalvik.system.NativeStart.main(Native Method)
  Caused by: java.lang.StringIndexOutOfBoundsException: length=9; regionStart=0; regionLength=-1
  at java.lang.String.startEndAndLength(String.java:593)
  at java.lang.String.substring(String.java:1474)
  at com.ootpapps.saving.made.simple.App.formatCurrencyRound(App.java:68)
  at com.ootpapps.saving.made.simple.SavingsPlanOverview.updateSavingsPlanWidgets(SavingsPlanOverview.java:224)
  at com.ootpapps.saving.made.simple.SavingsPlanOverview.onResume(SavingsPlanOverview.java:156)
  at android.app.Instrumentation.callActivityOnResume(Instrumentation.java:1184)
  at android.app.Activity.performResume(Activity.java:5082)
  at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2565)
  ... 12 more
  </code></pre>
  
  <p>If someone could propose a better way to round this currency string, or, a better way to round the raw number into a formatted string with no decimals, I'd hugely appreciate it. And again, this only fails on GNexus running 4.1.2 for some reason.</p>
  
  <p>Thanks!</p>
  
  <p>Edit Added formatCurrency code:</p>
  
  <pre><code>public static String formatCurrency(double amount) {
      NumberFormat currency = NumberFormat.getCurrencyInstance();
  
      return currency.format(amount);
  }
  </code></pre>
  
  <p>Edit cont'd - <strong>Code I use which works to round currency to whole dollars</strong>:</p>
  
  <pre><code>public static String formatCurrencyWhole(double amount) {
      NumberFormat currency = NumberFormat.getCurrencyInstance();
      currency.setMaximumFractionDigits(0);
      currency.setMinimumFractionDigits(0);
  
      return currency.format(amount);
  }
  </code></pre>