I am working currently with PHP validation of decimal values. The function works well in determining decimals/integers from letters but my goal is to format the decimal/integer values into proper currency values such as $0.00.
How can I format the input value into currency format with the dollar sign such as $0.00?
<?
if (isset($_POST['price'])){
$price = $_POST['price'];
$priceString = empty($price['price'])?null:trim($price['price']);
if(!empty($priceString)) {
if (preg_match('/^[+\-]?\d+(\.\d+)?$/', $price)){
echo ('<div id="price_input"><span id="resultval">'.$price.'</span></div>');
}
else {
echo ('<div id="price_input"><span id="resultval">Please input a valid decimal number.</span></div>');
}
}
else {
echo '';
}
}
?>
Use the money_format function:
outputs: $5.2
EDIT: