Return to Snippet

Revision: 24107
at March 2, 2010 13:12 by jmiller


Updated Code
<?php
	// price(15)	returns $15
	// price(14.95)	returns $14.95

	function price($price, $decimals=null, $sign='$') {
		$price = (float) str_replace($sign, '', $price);	// Make sure price is float
		
		if ($decimals !== null) {
			if (is_numeric($decimals)) {
				return $sign.number_format($price, $decimals);
			} else {
				return $sign.number_format($price, 2);
			}
		}

		if (round($price) == $price) {
			return $sign.$price;
		} else {
			return $sign.number_format($price,2);
		}
	}
?>

Revision: 24106
at March 2, 2010 12:56 by jmiller


Updated Code
<?php
	// price(15)	returns $15
	// price(14.95)	returns $14.95

	function price($price, $decimals=null, $sign='$') {
		$price = (int) str_replace($sign, '', $price);	// Make sure price is integer
		
		if ($decimals !== null) {
			if (is_numeric($decimals)) {
				return $sign.number_format($price, $decimals);
			} else {
				return $sign.number_format($price, 2);
			}
		}

		if (round($price) == $price) {
			return $sign.$price;
		} else {
			return $sign.number_format($price,2);
		}
	}
?>

Revision: 24105
at March 2, 2010 12:39 by jmiller


Updated Code
<?php
	// price(15)	returns $15
	// price(14.95)	returns $14.95

	function price($price, $sign='$') {
		$price = str_replace($sign, '', $price);	// Strip out sign from price (if necessary)
		if (round($price) == $price) {
			return $sign.$price;
		} else {
			return $sign.number_format($price,2);
		}
	}
?>

Revision: 24104
at February 21, 2010 14:04 by jmiller


Initial Code
<?php
	// price(15)	returns $15
	// price(14.95)	returns $14.95

	function price($price, $sign='$') {
		if (round($price) == $price) {
			return $sign.$price;
		} else {
			return $sign.number_format($price,2);
		}
	}
?>

Initial URL


Initial Description
Will strip off the decimals of a number (price) unless they are necessary.

Initial Title
Price without decimals unless necessary

Initial Tags
number

Initial Language
PHP