javascript parseDecimal number format with decimal

No Comments

Want a number_format function or decimal display in javascript, try this

function parseDecimal(d, zeros, trunc) {
d=d.replace(/[a-zA-Z\!\@\#\$\%\^\&\*\(\)\_\+\-\=\{\}\|\[\]\\\:\"\;'\<\>\?\,\/\~\`]/g,"");
while (d.indexOf(".") != d.lastIndexOf("."))
d=d.replace(/\./,"");
if (typeof zeros == 'undefined' || zeros == "") {
return parseFloat(d);
} else {
var mult = Math.pow(10,zeros);
if (typeof trunc == 'undefined' || (trunc) == false)
return parseFloat(Math.round(d*mult)/mult);
else
return parseFloat(Math.floor(d*mult)/mult);
}
}

Javascript ForEach Equivalent – JS

No Comments

One thing with the For Loop in JavaScript is it doesn’t seem to be very well documented that you can use it to do an equivalent of a ForEach loop.

Here’s a short example of doing the ForEach loop equivalent in JavaScript:

var colors = ['Red','Green','Blue'];

for ( var i in colors )
{
alert( colors[i] );
}

In the above code, the variable "i" is our iterator and by using the "in" keyword the "for" loop actually loops through all elements in the Array for us. Using this you no longer have to worry about the length of the array.