(New page: <source lang="java"> public static String convertDoubleToString(double x, int w, int d) { if (Double.isNaN(x)) { return "NaN"; } java.text.DecimalFormat fmt = new java.text.DecimalFor...)
 
 
Line 26: Line 26:
 
}
 
}
 
</source>
 
</source>
 +
  
 
[[JavaHowTo|Back to JavaHowTo]]
 
[[JavaHowTo|Back to JavaHowTo]]

Latest revision as of 04:21, 23 November 2010

public static String convertDoubleToString(double x, int w, int d) {
	if (Double.isNaN(x)) {
		return "NaN";
	}
	java.text.DecimalFormat fmt = new java.text.DecimalFormat();
	fmt.setMaximumFractionDigits(d);
	fmt.setMinimumFractionDigits(d);
	fmt.setGroupingUsed(false);
	StringBuilder s = new StringBuilder(fmt.format(x));
	while (s.length() < w) {
		s.insert(0, ' ');
	}
	return s.toString();
}
 
public static String convertDoubleToString(double x) {
	if (Ts.mathIsInteger(x)) {
		return Ts.convertIntToString((int) x);
	}
	return convertDoubleToString(x, 5, 2);
}
 
public static String convertIntToString(int number) {
	return String.valueOf(number);
}


Back to JavaHowTo

Alumni Liaison

Ph.D. 2007, working on developing cool imaging technologies for digital cameras, camera phones, and video surveillance cameras.

Buyue Zhang