-->

Format Decimal Values Correctly Using Java

If you happen to come across a decimal value in Java e.g. 0.0008689 but is displayed in a weird way like 8.689E-4, you can use this method to format it in a more readable display. Pretty useful when formatting currencies.

1
2
3
4
5
public static String formatRate(double rate, NumberFormat nf) {
   DecimalFormat df = (DecimalFormat) nf;
   df.applyPattern("#,###,###.####################");
   return df.format(rate);
}

Found this post useful? Donations appreciated. Every little $ helps.

Related Posts with Thumbnails

tags: ,

Leave a Reply

Spam protection by WP Captcha-Free