Code of the day: Converts Bytes to Simplify Units


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

A few days ago I noticed that my “code of the day” article was wrong. So I spent some time fixing it and provided test cases to check my work.
The main difference about this script is that it allows you to support both the SI and IEC standard and fixes a few rounding errors.

// function: getBytesWithUnit
// input: bytes (number)
// input: useSI (boolean), if true then uses SI standard (1KB = 1000bytes), otherwise uses IEC (1KiB = 1024 bytes)
// input: precision (number), sets the maximum length of decimal places.
// input: useSISuffix (boolean), if true forces the suffix to be in SI standard. Useful if you want 1KB = 1024 bytes
// returns (string), represents bytes is the most simplified form.
var getBytesWithUnit = function (bytes, useSI, precision, useSISuffix) {
	"use strict";
	if (!(!isNaN(bytes) && +bytes > -1 && isFinite(bytes))) {
		return false;
	}
	var units, obj,	amountOfUnits, unitSelected, suffix;
	units = ['bytes', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'];
	obj = {
		base : useSI ? 10 : 2,
		unitDegreeDiff : useSI ? 3 : 10
	};
	amountOfUnits = Math.max(0, Math.floor(Math.round(Math.log(+bytes) / Math.log(obj.base) * 1e6) / 1e6));
	unitSelected = Math.floor(amountOfUnits / obj.unitDegreeDiff);
	unitSelected = units.length > unitSelected ? unitSelected : units.length - 1;
	suffix = (useSI || useSISuffix) ? units[unitSelected] : units[unitSelected].replace('B', 'iB');
	bytes = +bytes / Math.pow(obj.base, obj.unitDegreeDiff * unitSelected);
	precision = precision || 3;
	if (bytes.toString().length > bytes.toFixed(precision).toString().length) {
		bytes = bytes.toFixed(precision);
	}
	return bytes + " " + suffix;
};



Demo: Test Script here

Want to learn more about Javascript?
Check out this “Professional JavaScript for Web Developers”.

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

js2coffee.org helps your learn coffeescript faster


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: 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

coffeescript icon
coffeescript icon

Coffee-script is a smoking hot simple language that converts 100% into Javascript code. Coffee-script allows you to spend more time focusing on the algorithm rather than the syntax.
js2coffee.org is an awesome website that converts your Coffee-script to Javascript and vice versa on the fly. So you can learn Coffee-script faster.
Example:
Let’s convert my Random Number Generator code from a previous post to Coffee-script.

// Javascript version
var getRandomNum = function( i, decLen ){
	return (Math.random() * (i||1) ).toFixed(decLen);
};

# My Coffee-script version
getRandomNum = ( i=1, decLen ) -> (Math.random()*i).toFixed(decLen)

# js2coffee.org Coffee-script verison
getRandomNum = (i, decLen) ->
	(Math.random() * (i or 1)).toFixed decLen

Awesome huh?

Check out a demo of Coffeescript here.

I’ll like to leave you with a HTML5 Coffee-script presentation.

Cheers.

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

Simple fix for MSI Windows Installer Error

Here’s a quick post over how to install applications that encounter the Windows Installer Error.


Error Message:

Windows Installer
This installation package could not be opened.
Verify that the package exists and that you can access it, or contact the application vendor to verify that this is a valid Windows Installer package.

Alternative Solutions not presented in the video.
1) Avoid msi files and go for an alternative file format to install your application.
2) Install the application using the hidden Administrator account.
Here’s a Guide
3) Reinstall Windows on your computer.
4) Re-download the file from a different website.
5) System Restore to a period where the problem didn’t exist.
6) Extract the content of the MSI file and run the executable from there.
Extract MSI contents.batch

REM Extracts the filename.msi file to C:\extracted_files
msiexec /a filename.msi /qb TARGETDIR=C:\extracted_files

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

Check out this awesome Coffeescript Tutorial



http://bodil.github.com/coffeescript/

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