# Converting decimal number to ternary number

**URL:** https://discourse.processing.org/t/converting-decimal-number-to-ternary-number/45658
**Category:** Beginners
**Created:** [January 26, 2025, 11:31am UTC](https://discourse.processing.org/t/converting-decimal-number-to-ternary-number/45658 "2025-01-26T11:31:12Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![paulstgeorge](https://avatars.discourse-cdn.com/v4/letter/p/53a042/32.png) [@paulstgeorge](https://discourse.processing.org/u/paulstgeorge)
#### Post date: [January 26, 2025, 11:31am UTC](https://discourse.processing.org/t/converting-decimal-number-to-ternary-number/45658/1 "2025-01-26T11:31:12Z")

</div>

So here is my attempt (below). Sadly it does not work:  
`Void methods cannot return a value`

I am trying to use a recursive function. Can it be done?

```auto
void setup() {
  noLoop();
}

void draw() {
  for (int i = 0; i < 34; i++) {
    String t = tern(i);
    Println(t);
  }
}

String tern(int dec) {
  float a = dec/3;
  int e = floor(a);

  int q = dec % 3;

  if (dec == 0) {
    return str(0);
  } else if (e == 0) {
    return str(q);
  } else {
    return tern(e) + str(q);
  }
}

```

---

<div class="post-metadata">

### Author: ![eightohnine](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/eightohnine/32/19793_2.png) [@eightohnine](https://discourse.processing.org/u/eightohnine)
#### Post date: [January 26, 2025, 11:55am UTC](https://discourse.processing.org/t/converting-decimal-number-to-ternary-number/45658/2 "2025-01-26T11:55:40Z")

</div>

I’m not getting that error you are reporting.  
Though there is an issue with `Println(t);` – an errant capital P. Correcting that runs the sketch with no problems and produces an output. Admittedly, I have not evaluated the results to check if your conversion is fully correct.

---

<div class="post-metadata">

### Author: ![paulstgeorge](https://avatars.discourse-cdn.com/v4/letter/p/53a042/32.png) [@paulstgeorge](https://discourse.processing.org/u/paulstgeorge)
#### Post date: [January 26, 2025, 12:00pm UTC](https://discourse.processing.org/t/converting-decimal-number-to-ternary-number/45658/3 "2025-01-26T12:00:37Z")

</div>

Doh. That has solved it. Thank you.

The results look good to me, but…
