Tag Archive for number

Millisecond Equivalent of Time Interval

//Gets the millisecond equivalent of an interval
//Example: (2).seconds() == 2000
Number.prototype.seconds = function(){ return this * 1000; };
Number.prototype.minutes = function(){ return this * 60000; };		//60 * 1000
Number.prototype.hours = function(){ return this * 3600000; };		//60 * 60 * 1000
Number.prototype.days = function(){ return this * 86400000; };		//24 * 60 * 60 * 1000
Number.prototype.weeks = function(){ return this * 604800000; };	//7 * 24 * 60 * 60 * 1000

source

Shortest 4 digit PIN’s (1000 – 9999)

<?=ltrim(chunk_split(str_pad(implode(range(1000, 9999)), 6, ", ", STR_PAD_LEFT), 4), ', ');?>

source

Even Shorter 4 digit PIN’s

<?
echo '0000';
for($i = 1; $i < 10000; $i++)
echo ', '.str_repeat("0",(4-strlen($i))).$i;
?>

source

Shorter 4 digit PIN’s

<?
$i = 0;
while($i < 10000)
{
echo str_repeat("0",(4-strlen($i))).$i;
if($i != 9999) echo ', ';
$i++;
}
?>

source

4 digit PIN’s

<?
$i = 0;
$pins = '';
while($i < 10000)
{
if(strlen($i) == 1) $add = '000';
if(strlen($i) == 2) $add = '00';
if(strlen($i) == 3) $add = '0';
if(strlen($i) == 4) $add = '';
$pins .= ', '.$add.$i;
$i++;
}
echo ltrim($pins, ', ');
?>

source

Pad single digit number to 2.

$padded_num = str_pad($num, 2, 0, STR_PAD_LEFT);

source

Turn 10000000 into 10.000.000

public function setThousands(value:String):String
{
var pattern:RegExp = /(d+)(d{3}(.|,|$))/gi;
var str:String = value;

while (str.match(pattern).length != 0)
{
str = (str.replace(pattern, "$1.$2"));
}

return str;
}

source

Number Base Conversion

//Convert a number to a different base (e.g., from hex to decimal)
function changeBase(num, from, to)
{
if(isNaN(from) || from < 2 || from > 36 || isNaN(to) || to < 2 || to > 36)
throw (new RangeError("Illegal radix. Radices must be integers between 2 and 36, inclusive."));
num = parseInt(num, from);	//convert to decimal
num = num.toString(to);	//convert the decimal to desired base
return num.toUpperCase();
}

source

Format number with commas every 3 decimal places

function add_commas(nStr)
{
nStr += '';
x = nStr.split('.');
x1 = x[0];
x2 = x.length > 1 ? '.' + x[1] : '';
var rgx = /(d+)(d{3})/;
while (rgx.test(x1)) {
x1 = x1.replace(rgx, '$1' + ',' + '$2');
}
return x1 + x2;
}

source

Dump a file to the terminal, starting at some line number

tail -n+99 foo.txt | more

source