Convert a string / number to float

Categories:

function convert_to_money( $s, $dec=2 )
{
  // converts a string / number to float  
  $f = floatval( $s );
  if( $f<0 ): // if negative
    $p = array( '($', ')' ); // formatting negative numbers
    $f = str_replace( '-', '', $f ); // removes the minus sign
  else: // positive no.
    $p = array( '$', NULL );
  endif;
  return $p[0].number_format( $f, $dec ).$p[1];
}