phpjs
Version:
86 lines (72 loc) • 2.25 kB
Markdown
layout: page
title: "JavaScript decbin function"
comments: true
sharing: true
footer: true
alias:
- /functions/view/decbin:381
- /functions/view/decbin
- /functions/view/381
- /functions/decbin:381
- /functions/381
<!-- Generated by Rakefile:build -->
A JavaScript equivalent of PHP's decbin
{% codeblock math/decbin.js lang:js https://raw.github.com/kvz/phpjs/master/functions/math/decbin.js raw on github %}
function decbin (number) {
// From: http://phpjs.org/functions
// + original by: Enrique Gonzalez
// + bugfixed by: Onno Marsman
// + improved by: http://stackoverflow.com/questions/57803/how-to-convert-decimal-to-hex-in-javascript
// + input by: pilus
// + input by: nord_ua
// * example 1: decbin(12);
// * returns 1: '1100'
// * example 2: decbin(26);
// * returns 2: '11010'
// * example 3: decbin('26');
// * returns 3: '11010'
if (number < 0) {
number = 0xFFFFFFFF + number + 1;
}
return parseInt(number, 10).toString(2);
}
{% endcodeblock %}
- [Raw function on GitHub](https://github.com/kvz/phpjs/blob/master/functions/math/decbin.js)
Please note that php.js uses JavaScript objects as substitutes for PHP arrays, they are
the closest match to this hashtable-like data structure.
Please also note that php.js offers community built functions and goes by the
[McDonald's Theory](https://medium.com/what-i-learned-building/9216e1c9da7d). We'll put online
functions that are far from perfect, in the hopes to spark better contributions.
Do you have one? Then please just:
- [Edit on GitHub](https://github.com/kvz/phpjs/edit/master/functions/math/decbin.js)
### Example 1
This code
{% codeblock lang:js example %}
decbin(12);
{% endcodeblock %}
Should return
{% codeblock lang:js returns %}
'1100'
{% endcodeblock %}
### Example 2
This code
{% codeblock lang:js example %}
decbin(26);
{% endcodeblock %}
Should return
{% codeblock lang:js returns %}
'11010'
{% endcodeblock %}
### Example 3
This code
{% codeblock lang:js example %}
decbin('26');
{% endcodeblock %}
Should return
{% codeblock lang:js returns %}
'11010'
{% endcodeblock %}
### Other PHP functions in the math extension
{% render_partial _includes/custom/math.html %}