# Formating a Binary number to include leading zeros

**URL:** <https://forum.inductiveautomation.com/t/formating-a-binary-number-to-include-leading-zeros/44642>\
**Category:** Ignition\
**Tags:** ignition79, ignition80\
**Created:** [March 18, 2021, 6:14pm UTC](https://forum.inductiveautomation.com/t/formating-a-binary-number-to-include-leading-zeros/44642 "2021-03-18T18:14:07Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![michael.ruel](https://avatars.discourse-cdn.com/v4/letter/m/ecd19e/32.png) [@michael.ruel](https://forum.inductiveautomation.com/u/michael.ruel)\
**Post date:** [March 18, 2021, 6:14pm UTC](https://forum.inductiveautomation.com/t/formating-a-binary-number-to-include-leading-zeros/44642/1 "2021-03-18T18:14:07Z")

</div>

I am trying to have a number convert to the binary number in order to take substrings of the binary for doing a Red,Green,Blue decimal value in expressions. I’ve almost got it, but needed the leading zeros. I am using the numberFormat which mostly works, but ran into something unexpected. the following number toBinary(2939647) works just fine and yields 1011001101101011111111 missing the first two zero’s, however the following "numberFormat(toBinary(2939647),“000000000000000000000000”) yields  
001011001101101011200000 which is not correct. the numberFormat rounded up to 2 for some reason. Does anyone have a better way to convert to a binary representation and have the correct number of bits showing?

---

<div class="post-metadata">

**Author:** ![KathyApplebaum](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/kathyapplebaum/32/7261_2.png) [@KathyApplebaum](https://forum.inductiveautomation.com/u/KathyApplebaum)\
**Post date:** [March 18, 2021, 6:34pm UTC](https://forum.inductiveautomation.com/t/formating-a-binary-number-to-include-leading-zeros/44642/2 "2021-03-18T18:34:43Z")

</div>

toBinary already returns a string, so I wouldn’t expect numberFormat to work correctly with it.

You’d probably need to take the result of toBinary and concat the right number of leading zeros based on the length of the string.

---

<div class="post-metadata">

**Author:** ![michael.ruel](https://avatars.discourse-cdn.com/v4/letter/m/ecd19e/32.png) [@michael.ruel](https://forum.inductiveautomation.com/u/michael.ruel)\
**Post date:** [March 18, 2021, 6:45pm UTC](https://forum.inductiveautomation.com/t/formating-a-binary-number-to-include-leading-zeros/44642/3 "2021-03-18T18:45:57Z")

</div>

Yeah that’s what I was going to try next. Thank you.

---

<div class="post-metadata">

**Author:** ![michael.ruel](https://avatars.discourse-cdn.com/v4/letter/m/ecd19e/32.png) [@michael.ruel](https://forum.inductiveautomation.com/u/michael.ruel)\
**Post date:** [March 18, 2021, 7:32pm UTC](https://forum.inductiveautomation.com/t/formating-a-binary-number-to-include-leading-zeros/44642/5 "2021-03-18T19:32:25Z")

</div>

Here’s what I ended up doing that worked. Thanks for the help.

if(len(toBinary(Number))=24,toBinary(Number),  
concat(repeat(‘0’,24-len(toBinary(Number))),toBinary(Number)))

---

<div class="post-metadata">

**Author:** ![paul-griffith](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/paul-griffith/32/37249_2.png) [@paul-griffith](https://forum.inductiveautomation.com/u/paul-griffith)\
**Post date:** [March 18, 2021, 10:34pm UTC](https://forum.inductiveautomation.com/t/formating-a-binary-number-to-include-leading-zeros/44642/6 "2021-03-18T22:34:49Z")

</div>

Maybe more clear, almost certainly a lot faster:  
`replace(stringFormat("%24s", toBinary({Root Container.Spinner.intValue})), " ", "0")`
