KDOT KDOT - 7 days ago 5
PHP Question

Convert 4 digit integer to decimal for money

I am trying to convert a 4 digit integer like

1999
to
19.99
.

I have looked at functions like
money_format()
but this seems to not be what I am looking for. Here is a function I wrote but there must be pre-existing method to do this?

function numberToDecimal((int)$num) {
if(!preg_match('/^\d{4}$/', $num) return;
$parsed = (string)$num;
$i = 0;
$ret = $parsed[$i++] . $parsed[$i++] . '.';
$ret .= $parsed[$i++] . $parsed[$i];
return $ret;
}


Thank-you in advance.

See the code working over at corresponding to the answer 3v4l.

Answer

What about the following:

$money = 1999 ;
echo number_format(($money /100), 2, '.', ' ');
// Will output: 19.99