Archive for the ‘Web’ Category

Round Half To Even With Javascript

Monday, November 30th, 2009

The following script provides an easy way to perform unbiased rounding, convergent rounding, statistician’s rounding, Dutch rounding, Gaussian rounding, or bankers’ rounding using Javascript.

Examples

var numToRound = 10.005;
document.write(numToRound.rhte(0.01);
result: 10.00

var numToRound = 10.0015;
document.write(numToRound.rhte(0.001);
result: 10.002

var numToRound = 10.00500000001;
document.write(numToRound.rhte(0.01);
result: 10.01

var numToRound = 1005001;
document.write(numToRound.rhte(10000);
result: 1010000

/*
	Round Half To Even (rhte) extends the 'Number' and 'String' objects

	Argument(placeToRound) examples: 0.0001,0.01,1,10,100

	Usage:
		Number.rhte(rounding value)
			(124.34450000).rhte(0.001) - [Result: 124.344]
			(124.34450001).rhte(0.001) - [Result: 124.345]
			(124.34550000).rhte(0.001) - [Result: 124.346]

		String.rhte(rounding value)
			'124.34450000'.rhte(0.001) - [Result: 124.344]
			'124.34450001'.rhte(0.001) - [Result: 124.345]
			'124.34550000'.rhte(0.001) - [Result: 124.346]
*/
function rhte(placeToRound){
	var pToRnd = parseFloat(placeToRound);
	var decNum = parseFloat(this)/pToRnd;
	var floor = Math.floor(decNum);
	var dec = decNum - floor;
	var floor,oddEven,places;
	parseFloat(this) > 0 ? floor = Math.floor(decNum) : floor = Math.ceil(decNum);
	pToRnd < 1 ? places = pToRnd.toString().split('.')[1].length : places = 0;
	floor % 2 == 1 ? oddEven = 1 : oddEven = 0;
	if(dec != 0.5){
		return ((Math.round(decNum)) * pToRnd).toFixed(places);
	}
	else{
		if(oddEven == 1){
			return ((floor + 1) * pToRnd).toFixed(places);
		}
		else{
			return (floor * pToRnd).toFixed(places);
		}
	}
}

Number.prototype.rhte = rhte;
String.prototype.rhte = rhte;

Wordpress postMash Widget

Tuesday, July 21st, 2009

I really like the postMash plugin for Wordpress. It allows you to drag and drop your posts into any order. This is great when using posts as events. You can put them into the order that they’ll occur rather than the order that you posted them. The only problem is that the Recent Posts widget won’t match your post order once you’ve re-ordered them.

Here I’ve modified the Recent Posts widget (file: /wp-includes/default-widgets.php) so that it’ll work with this plugin.

Insert the following code after line #545 ($r = new WP_Query(array(’showposts’ =>…)(WP ver-2.8)

$r->set('orderby', 'menu_order');
$r->set('order', 'ASC');
$r->get_posts();

jQuery Classes – An Object Oriented Approach

Tuesday, April 14th, 2009

MooTools has been my framework of choice for the past couple of years. I’ve worked my way up to using Classes for most of my projects and really liked using the OOP approach. For the past couple of projects, I started using Wordpress as my CMS and every time ended up using jQuery. I was disappointed to find out that it didn’t have the native Class library that MooTools did. Fortunately there are a couple of plugins available to assist with this task:

- A plugin that adds class functionality to jQuery: jQuery-Klass

- John Resig himself wrote this library/plugin: Classy Query