note 17090 added to ref.strings

From: Date: Mon, 26 Nov 2001 05:23:35 +0000
Subject: note 17090 added to ref.strings
Groups: php.notes 
Request: Send a blank email to php-notes+get-21273@lists.php.net to get a copy of this message
i needed to change a number into currency with properly placed ','. I came up with the following and figured id post it in case anyone else needed to do the same. <? //just a random number $num = '225135436'; //reverse the number so you can count //every three digits and insert a ',' $num = strrev($num); //$insert_comma prevents code from continually entering the first //if statement after an insert of ',' because it reduces $i back //to a number that returns 0 from $i % 3 $insert_comma = 0; for( $i = 0; $i < strlen($num); $i++) { if ( ( !($i % 3) ) && ( $insert_comma ) ) { // time for a comma $money = $money . ','; $i--; $insert_comma = 0; } else { //just move the next int over $money = $money . substr( $num, $i, 1 ); $insert_comma = 1; } } $money = $money . "$"; $money = strrev($money); $money = $money . ".00"; echo strrev($num) . "\n" . $money . "\n"; ?> -- http://www.php.net/manual/en/ref.strings.php http://master.php.net/manage/user-notes.php?action=edit+17090 http://master.php.net/manage/user-notes.php?action=delete+17090 http://master.php.net/manage/user-notes.php?action=reject+17090

« previous php.notes (#21273) next »