Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.