Here you can find the source of formatNumber(String number, int maxFractionalDigits)
public static String formatNumber(String number, int maxFractionalDigits)
//package com.java2s; //License from project: Open Source License import java.text.NumberFormat; public class Main { /**//from w ww . j ava 2s.co m * Formats the provided number with the specified number of fractional digits * If number arg is not a number, returns the String */ public static String formatNumber(String number, int maxFractionalDigits) { try { NumberFormat nf = NumberFormat.getNumberInstance(); nf.setMaximumFractionDigits(maxFractionalDigits); return nf.format(Double.parseDouble(number)); } catch (Exception ex) { return number; } } public static String formatNumber(double number, int maxFractionalDigits) { try { NumberFormat nf = NumberFormat.getNumberInstance(); nf.setMaximumFractionDigits(maxFractionalDigits); return nf.format(number); } catch (Exception ex) { return String.valueOf(number); } } }