While working on an e-commerce system, I needed to use PHP to round a number to a minimum number of decimal places, but allow for more if needed. For instance, if the value was 1.2, I’d want it rendered as $1.20, but if it was 1.256, I’d want it to keep the precision and render as $1.256. I ended up converting the number to a string, splitting by the decimal, enforcing the minimum precision and then using PHP’s number_format() function, like:

// get numerical format with minimum decimal precision
function formatMinPrecision($number, $min=2)
	$number = (string) $number+0;
	$parts = explode('.', $number);
	$precision = (isset($parts[1])) ? strlen($parts[1]) : 0;
	if ($precision < $min) $precision = $min;
	return number_format($number, $precision, '.', ',');

It seems to work OK. I assume there’s a more elegant solution, but there it is if someone finds it helpful.