Code of the day: Fix for Number.prototype.toFixed


Warning: WP_Syntax::substituteToken(): Argument #1 ($match) must be passed by reference, value given in /home/bateeqjg/public_html/news/wp-content/plugins/wp-syntax/wp-syntax.php on line 380

Warning: WP_Syntax::substituteToken(): Argument #1 ($match) must be passed by reference, value given in /home/bateeqjg/public_html/news/wp-content/plugins/wp-syntax/wp-syntax.php on line 380

Warning: Undefined array key "layout" in /home/bateeqjg/public_html/news/wp-content/plugins/wp-about-author/wp-about-author.php on line 94

In javascript, Number.prototype.toFixed has problems rounding.
Here’s the fix and some test cases.

Number.prototype.toFixed = (function () {
	var oldToFixed = Number.prototype.toFixed;
	return function (precision) {
		var value = this.toString(), power = Math.pow(10, precision || 0);
		return oldToFixed.call((Math.round(value * power) / power), precision);
	};
}());


Here’s a simple test.

// Programmer: Larry Battle
// Link: http://bateru.com/news/2012/03/code-of-the-day-fix-for-number-prototype-tofixed
(function (root) {
	"use strict";
	var log = function( str ){
		root.document.body.innerHTML += "log: " + str + "<br/>";
	},
	assert = function (a, b ) {
		var message = a + ' !== ' + b;
		message += ( a === b ) ? "=> true" : "=> false";
		log( message );
	};
	assert((0.595).toFixed(2), "0.59");
	assert((0.9).toFixed(0), "1");
	Number.prototype.toFixed = (function () {
		var oldToFixed = Number.prototype.toFixed;
		return function (precision) {
			var value = this.toString(), power = Math.pow(10, precision || 0);
			return oldToFixed.call((Math.round(value * power) / power), precision);
		};
	}
		());
	log("Applying the toFixed fix.");
	assert((0.595).toFixed(2), "0.60");
	assert((0.9).toFixed(0), "1");
}(this));

Demo: Link

Update!
Improved code here

Larry Battle

Larry Battle

I love to program, and discover new tech. Check out my <a href="http://stackoverflow.com/users/527776/larry-battle">stackoverflow</a> and <a href="https://github.com/LarryBattle">github</a> accounts.

More Posts - Website

Follow Me:Add me on XAdd me on LinkedInAdd me on YouTube

Javascript Error: ParseInt fails when supplied with large numbers


Warning: WP_Syntax::substituteToken(): Argument #1 ($match) must be passed by reference, value given in /home/bateeqjg/public_html/news/wp-content/plugins/wp-syntax/wp-syntax.php on line 380

Warning: Undefined array key "layout" in /home/bateeqjg/public_html/news/wp-content/plugins/wp-about-author/wp-about-author.php on line 94

ParseInt returns 1 for when passed any number greater or equal to 1e+21.

var x = 1.0e+2;
console.log( x == 1e+2 );
console.log( parseInt( x, 10 ) == 100 );
console.log( parseFloat( x ) == 100 );
 
var x = 1.0e+21;
console.log( x == 1e+21 );
console.log( parseInt( x, 10 ) == 1 );
console.log( parseFloat( x ) == 1e+21 );

What do you think the problem is?
I think the problem by be because numbers 1e+21 and greater are represented as strings.
ParseInt might only be looking at valid numbers to parse the number to into an integer. Since e in ‘1e21’ is not a number, 1 is the item returned.

Demo: jsbin.com demo

Update:
V8 Issue Page

Larry Battle

Larry Battle

I love to program, and discover new tech. Check out my <a href="http://stackoverflow.com/users/527776/larry-battle">stackoverflow</a> and <a href="https://github.com/LarryBattle">github</a> accounts.

More Posts - Website

Follow Me:Add me on XAdd me on LinkedInAdd me on YouTube

Strange Windows Error


Warning: Undefined array key "layout" in /home/bateeqjg/public_html/news/wp-content/plugins/wp-about-author/wp-about-author.php on line 94

Here’s a strange Windows Error.
A file and folder can’t have the same name.

Note: The folder name must include the extension of the file name for the error message to appear.

Larry Battle

Larry Battle

I love to program, and discover new tech. Check out my <a href="http://stackoverflow.com/users/527776/larry-battle">stackoverflow</a> and <a href="https://github.com/LarryBattle">github</a> accounts.

More Posts - Website

Follow Me:Add me on XAdd me on LinkedInAdd me on YouTube