Archive for the ‘Javascript’ Category

Javascript search / find in array

var test_array = new Array(’jack’, ’joey’, ’phillip’, ’peter’, ’jane’);
var assoc_array = new Array();
assoc_array[’sarah’] = 25;
assoc_array[’samantha’] = 22;
assoc_array[’joseph’] = 26;

JavaScript function to round numbers

var fbShare = {url: ‘http://sree.cc/javascript/javascript-function-to-round-numbers’,size:’large’} Here is a quick JavaScript function that can be used to round numbers to any precision:
Usage:
var num = 8.3897086546;
alert(roundFloat(num, 2));
alert(roundFloat(num, 4));

Code:

/* JavaScript function to round off numbers */
function roundFloat(num, precision)
{
if(num.toFixed)
{
num_round = num.toFixed(precision);
}
else
{
var tenths = 1;
for( var i=0; i<precision; i++ )
{
tenths = tenths * 10;
}
num_round = Math.round(num*tenths)/tenths
}
if( isNaN(num_round) )
{
num_round = [...]