Alex Alex - 3 months ago 5
PHP Question

Add commas in amount using php

Please i have a jquery code to add commas in number at the right place now i don't want to use jquery for it again how do i do this using php to add commas?
bellow is my attempt

The jquery

<script type="text/javascript">
//<![CDATA[
$(window).load(function(){
function numberWithCommas(x) {
return x.toString().replace(/\B(?=(?:\d{3})+(?!\d))/g, ",");
}


$("#numbal").each(function(){
var bal_cc = $(this).html();
bal_cc = numberWithCommas(bal_cc);

$(this).html(bal_cc)

})


});//]]>

</script>


PHP Tried

<?php

function numberWithCommas(x) {
return x.str_replace(/\B(?=(?:\d{3})+(?!\d))/g, ",");
}
$bal = numberWithCommas($UserCradit);
echo $ubal;
?>

Answer

This is wrong: x.str_replace(/\B(?=(?:\d{3})+(?!\d))/g, ","); you should learn to use Php Manual before coming to stack overflow. number_format(http://php.net/manual/en/function.number-format.php) is what you are looking for

<?php
    $bal = number_format($UserCradit);
    echo $bal;
?>
Comments