PHP

HOW TO ADD COMMAS TO NUMBERS IN PHP

MINIMUM - SPECIFY NUMBER & DECIMAL PLACES  $num = number_format(1234.678, 2); 1,234.68 SPECIFY SEPARATORS $num = number_format(1234.678, 2, "!", " "); 1 234!68

NUMBER FORMAT

01

THE NUMBER $num = 1234.678;  INSERT COMMAS $pattern = "/\B(?=(\d{3})+(?!\d))/"; $replace = ","; $num = preg_replace($pattern, $replace, $num); echo $num; 1,234.678

REGULAR EXPRESSION

02

SPLIT WHOLE & DECIMAL NUMBERS  $num = explode(".", 1234.678);   ADD COMMAS TO WHOLE $num[0] = strrev($num[0]); $num[0] = str_split($num[0], 3); $num[0] = implode(",", $num[0]); $num[0] = strrev($num[0]); COMBINE BACK $num = "$arrange[0].$arrange[1]"; 1,234.678

MANUAL FORMAT

03