PHP - generating 4 digit figures adding comma(), & dot (.) figures

Asked By Olusegun A. on 07-Oct-09 06:46 AM

Hello my Success-Oriented Friends,

Firstly, how do i code my MT_RAND() command to generate just 4 digit numbers for my PIN and confirmation.

Secondly, how do i make the number entered into one of the fields in my form to appear as currency. I want dot (.) to com after the first two digits and comma(,) after every 3 digits for it to look like this (1,234,567.89).

Regards
Olusegun

generating 4 digit figures - Sagar P replied to Olusegun A. on 07-Oct-09 06:52 AM

If you want, say, a four digit PIN number, then you can say:

$pin = rand(1000, 9999);

Pins can also start with 0, i.e. 0123. A more correct response would be:

$pin=substr('0000' . rand(1, 9999), -4);

It prepends '0000' to a random number between 1 and 9999 then takes the last
four characters.

reply - Deepak Sonawane replied to Olusegun A. on 07-Oct-09 07:06 AM

Hi,
Code to generate just 4 digit numbers for any PIN:
$pin=substr('0000' . rand(1, 9999), -4);

It prepends '0000' to a random number between 1 and 9999 then takes the last
four characters.
Code to appear as currency:
function is_currency($v) {
//returns a string with only the numbers for a currency format
$v = str_replace("$", "", $v);
$regex = '/(?:([0-9]+)|.)/'
if(preg_match($regex, trim($v) ) ){
$v = $v + 0.00001;
if (is_float($v)) {
return round($v, 2);
}
}
else {
return 0;
}
}