Code of the day: Javascript convert bytes to KB, MB, GB, etc


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

Note: This uses the IEC standard. That means 1 KB = 1024, not 1000 like the SI standard.

/**
* @function: getBytesWithUnit()
* @purpose: Converts bytes to the most simplified unit.
* @param: (number) bytes, the amount of bytes
* @returns: (string)
*/
var getBytesWithUnit = function( bytes ){
	if( isNaN( bytes ) ){ return; }
	var units = [ ' bytes', ' KB', ' MB', ' GB', ' TB', ' PB', ' EB', ' ZB', ' YB' ];
	var amountOf2s = Math.floor( Math.log( +bytes )/Math.log(2) );
	if( amountOf2s < 1 ){
		amountOf2s = 0;
	}
	var i = Math.floor( amountOf2s / 10 );
	bytes = +bytes / Math.pow( 2, 10*i );
 
	// Rounds to 3 decimals places.
        if( bytes.toString().length > bytes.toFixed(3).toString().length ){
            bytes = bytes.toFixed(3);
        }
	return bytes + units[i];
};

Output:

console.log( getBytesWithUnit( ) );			// returns undefined.
console.log( getBytesWithUnit( 'non a number.' ) );	// returns undefined.
console.log( getBytesWithUnit( '123' ));		// returns '123 bytes'.
console.log( getBytesWithUnit( 1024 ));		// returns '1 KB'.
console.log( getBytesWithUnit( (1024 * 1024) + 1024 )); // returns '1.001 MB'.
console.log( getBytesWithUnit( 1024 * 1024 * 1024 )); // returns '1 GB'.
console.log( getBytesWithUnit( 1024 * 1024 * 64 )); // returns '64 MB'.

Update
I updated the script to support both the SI and IEC standard.
Check it out here http://bateru.com/news/2012/03/code-of-the-day-converts-bytes-to-unit/

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

Code of the day: Check to see if a element has a event.


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
// hasEvent checks to see if a element contains a event.
// @requires jQuery 1.3.2+
// @params el: string, jQuery object, node element.
// @params eventName: string, name of the event.
// @returns boolean
var hasEvent = function( el, eventName ){
	if( !$( el ).length || !$( el ).data( 'events' ) ){
		return false;
	}
	return !!$( el ).data( 'events' )[ eventName ];
};

Demo: (requires firebug or google chrome)

var el = $( '<div/>' ).click( $.noop );
console.clear();
console.log( hasEvent( el, 'click' ) );// returns true;
 
console.log( hasEvent( el, 'focusNow' ) );// returns false;
el.bind( 'focusNow', $.noop );
console.log( hasEvent( el, 'focusNow' ) );// returns true;
 
console.log( hasEvent( document.body, 'onload' ) );// returns false;
$(document.body).bind( 'onload', $.noop );
console.log( hasEvent( document.body, 'onload' ) );// returns true;
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

Code of the Day: Javascript Fibonacci Numbers the fast way


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
1
2
3
4
5
6
7
8
//Programmer: Larry Battle
//Date: April 18, 2011
//Purpose: Get Fibonacci numbers in O(1) time.
var getFibonacciNum = function( n ){
    var s5 = Math.sqrt(5);
    var phi1 = (1+s5)/2, phi2 = (1-s5)/2;
    return Math.round((Math.pow( phi1, n ) - Math.pow( phi2, n) )/s5);
};

Demo:

Output for 0 to 4

F(0) = 0
F(1) = 1
F(2) = 1
F(3) = 2
F(4) = 3
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

Code of the Day: Javascript Significant Figures


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

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

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

Did you know that JavaScript automatically trims off trailing zeros on numbers with decimals?
Well now you do, and in order to save precision you must wrap the number inside a string.
Example:

1
2
3
4
5
6
var a = "0.0100";
var b = 0.0100;                           // To save space the interpreter cuts off the last two zeros.
console.log( a === b.toString() );	// displays false because a = "0.0100" and b = 0.01
 
console.log( 1000 === 1000.000 );	// displays true
console.log( 0.1 === 0.1000 );		// displays true

Some might argue that you can use num.toPrecision() to save the trailing zeros. But what most are unaware of is that toPrecision() returns a string.

1
2
3
4
5
var num = (120.0).toPrecision(8);
console.log( num );                      // displays 120.00000
console.log( typeof num );            // displays string
console.log( +num );                    // displays 120
// +num is a shortcut for parseInt( num, 10);

Now let’s see what happens with leading zero’s.

1
2
3
4
5
6
7
//A leading 0 in front of a number converts it base 8.
// a.k.a. parseInt( num, 8);
console.log(0011);		// Displays 9
console.log( 010.1000 );		// Error! Octals can only be whole numbers.
console.log( 00000.1000 );	// Error for the same reason.
// The problem is that a leading zero will cause the number to be converted to octal.
// Thus, an error is generated because the decimal point is neither an operation nor semicolon.

Using numbers as strings can be useful when trying to find the Significant Digits, like in the following function.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
// Returns the significant digits of a number.
// @param {Number|String} num
// @return Returns -1 if invalid input, otherwise will return a positive number.
var getSigFigs = function (num) {
  if (!isFinite(Number(num))) {
    return -1;
  }
  var n = String(num).trim(),
  FIND_FRONT_ZEROS_SIGN_DOT_EXP = /^[\D0]+|\.|([e][^e]+)$/g,
  FIND_RIGHT_ZEROS = /0+$/g;
 
  if (!/\./.test(num)) {
    n = n.replace(FIND_RIGHT_ZEROS, "");
  }
  return n.replace(FIND_FRONT_ZEROS_SIGN_DOT_EXP, "").length;
};

Usage:

1
console.log( getSigFigFromNum( "0.01230" ) == 5);

Test cases

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
 
// Try using Qunit to run tests.
var assert = {};
assert.equal = function (a, b) {
  if (a != b) {
    console.error("FAIL: --> %s != %s", a, b);
  } else {
    console.log("Pass: %s == %s", a, b);
  }
};
var test = function (name, fn) {
  console.log("\n#Testing: %s", name);
  fn();
};
test("Invalid numbers", function () {
  assert.equal(getSigFigs("742400g"), -1);
  assert.equal(getSigFigs("g742400"), -1);
  assert.equal(getSigFigs("Infinity"), -1);
  assert.equal(getSigFigs("-Infinity"), -1);
  assert.equal(getSigFigs("NaN"), -1);
});
var testWithPrefixes = function (prefixes, nums, fn) {
  prefixes.forEach(function (prefix) {
    nums.forEach(function (num) {
      //assert.equal( "Testing " + expected + "" prefix+expected, fn(prefix+num));
    });
  });
};
test("Whole Numbers", function () {
  assert.equal(getSigFigs("742400"), 4);
  assert.equal(getSigFigs("742000"), 3);
  assert.equal(getSigFigs("740000"), 2);
  assert.equal(getSigFigs("700000"), 1);
});
test("Negative Whole Numbers", function () {
  assert.equal(getSigFigs("-742400"), 4);
  assert.equal(getSigFigs("-742000"), 3);
  assert.equal(getSigFigs("-740000"), 2);
  assert.equal(getSigFigs("-700000"), 1);
});
test("+Whole numbers", function () {
  assert.equal(getSigFigs("+742400"), 4);
  assert.equal(getSigFigs("+742000"), 3);
  assert.equal(getSigFigs("+740000"), 2);
  assert.equal(getSigFigs("+700000"), 1);
});
test("Decimals", function () {
  assert.equal(getSigFigs("0.07284"), 4);
  assert.equal(getSigFigs("0.0728"), 3);
  assert.equal(getSigFigs("0.072"), 2);
  assert.equal(getSigFigs("0.07"), 1);
  assert.equal(getSigFigs("123.07"), 5);
});
test("Negative Decimals", function () {
  assert.equal(getSigFigs("-0.07284"), 4);
  assert.equal(getSigFigs("-0.0728"), 3);
  assert.equal(getSigFigs("-0.072"), 2);
  assert.equal(getSigFigs("-0.07"), 1);
  assert.equal(getSigFigs("-10.07"), 4);
});
test("Decimals", function () {
  assert.equal(getSigFigs("+0.07284"), 4);
  assert.equal(getSigFigs("+0.0728"), 3);
  assert.equal(getSigFigs("+0.072"), 2);
  assert.equal(getSigFigs("+0.07"), 1);
});
test("Zero", function () {
  assert.equal(getSigFigs("0.0"), 0);
  assert.equal(getSigFigs("-0.0"), 0);
  assert.equal(getSigFigs("0"), 0);
  assert.equal(getSigFigs("+0"), 0);
  assert.equal(getSigFigs("-0"), 0);
});
 
test("E notated", function () {
  assert.equal(getSigFigs("1.3e32"), 2);
  assert.equal(getSigFigs("-1.23e-32"), 3);
  assert.equal(getSigFigs("-1.023e+120"), 4);
  assert.equal(getSigFigs("+5.023e+32"), 4);
  assert.equal(getSigFigs("+5.0230e+32"), 5);
});

Can you think of any other useful examples for number wrapped in strings?

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