Skip to content

Instantly share code, notes, and snippets.

@magician11
Created April 10, 2015 03:16
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save magician11/92af987c9632f53ff658 to your computer and use it in GitHub Desktop.
Save magician11/92af987c9632f53ff658 to your computer and use it in GitHub Desktop.
A negabinary filter for AngularJS
var negabinaryApp = angular.module('negabinaryApp', []);
negabinaryApp.filter('negabinary', function() {
return function (decimal) {
if (isNaN(decimal)) return "not a number";
var negabinary = [];
var base = -2;
var remainder;
while(decimal != 0) {
remainder = decimal % base;
decimal = Math.ceil(decimal / base);
negabinary.push(remainder >= 0 ? remainder : -remainder);
}
return negabinary.reverse().join('');
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment