
Untitled
By: a guest on
Jul 31st, 2012 | syntax:
None | size: 1.04 KB | hits: 9 | expires: Never
How do I amend an existing javascript to display currency values including cents?
function CurrencyFormatted(amount)
{
var i = parseFloat(amount);
if(isNaN(i)) { i = 0.00; }
var minus = '';
if(i < 0) { minus = '-'; }
i = Math.abs(i);
i = parseInt((i + .005) * 100);
i = i / 100;
s = new String(i);
if(s.indexOf('.') < 0) { s += '.00'; }
if(s.indexOf('.') == (s.length - 2)) { s += '0'; }
s = minus + s;
return s;
}
var n = 1.2345;
n.toFixed(2); // "1.23"
(1.234).toFixed(2); // "1.23"
(1.235).toFixed(2); // "1.24"
function CurrencyFormatted(amount)
{
return parseFloat(amount).toFixed(2);
}
function CurrencyFormatted(amount)
{
return (parseFloat(amount) + 0.005).toFixed(2);
}
<?php
$value = 5;
setlocale(LC_MONETARY, 'en_US');
echo '$'.money_format('%i', $value) . "n";//$5.00
$value = 5.545;
setlocale(LC_MONETARY, 'en_US');
echo '$'.money_format('%i', $value) . "n";//$5.54
$value = 9.99;
setlocale(LC_MONETARY, 'en_US');
echo '$'.money_format('%i', $value) . "n";//$9.99
?>