Friday, 15 May 2015

php - Convert ToMoney function not working -



php - Convert ToMoney function not working -

function not working prefix 8[any numbers].

input: 970 output:$9.70 input: 870 output:$870 input: 800 output:$800

function tomoney( $val, $symbol = '$', $r = 2 ) { $n = $val; $c = is_float($n) ? 1 : number_format( $n , $r ); $d = '.'; $t = ','; $sign = ( $n < 0 ) ? '-' : ''; $i = $n = number_format( abs( $n ), $r ); $j = ( ( $j = $i.length ) > 3 ) ? $j % 3 : 0; homecoming $symbol.$sign .( $j ? substr( $i, 0, $j) + $t : '').preg_replace('/(\d{3})(?=\d)/',"$1" + $t,substr($i,$j)) ; }

function taken from: money conversion not worked

the above error rectified in below

function tomoney( $val, $symbol = '$', $r = 2 ) { $n = $val; $c = is_float($n) ? 1 : number_format( $n , $r ); $d = '.'; $t = ','; $sign = ( $n < 0 ) ? '-' : ''; $i = $n = number_format( abs( $n ), $r ); $j = ( ( $j = strlen($i) ) > 3 ) ? $j % 3 : 0; homecoming $symbol.$sign .( $j ? substr( $i, 0, $j) + $t : '').preg_replace('/(\d{3})(?=\d)/',"$1" + $t,substr($i,$j)) ; }

if understand well, want 2 lastly numbers cents?

so why don't :

$val = number_format(intval(substr($val,0,strlen($val)-2)),).'.'.substr($val,-2);

the first substr forget 2 lastly digits , formated one thousand separator. sec substr take 2 lastly numbers.

php money

No comments:

Post a Comment