# How does converting unit works

**URL:** <https://community.glideapps.com/t/how-does-converting-unit-works/64172>\
**Category:** Ask for Help\
**Created:** [July 20, 2023, 2:32pm UTC](https://community.glideapps.com/t/how-does-converting-unit-works/64172 "2023-07-20T14:32:51Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Trustin](https://sea2.discourse-cdn.com/flex002/user_avatar/community.glideapps.com/trustin/32/88217_2.png) [@Trustin](https://community.glideapps.com/u/Trustin)\
**Post date:** [July 20, 2023, 2:32pm UTC](https://community.glideapps.com/t/how-does-converting-unit-works/64172/1 "2023-07-20T14:32:51Z")

</div>

I want to give it a try to convert 1 serving of fruits for example 1 cup into grams or even tablespoons.

Also is there a way to display “½ Cup” value in Glide?

---

<div class="post-metadata">

**Author:** ![gvalero](https://sea2.discourse-cdn.com/flex002/user_avatar/community.glideapps.com/gvalero/32/1037_2.png) [@gvalero](https://community.glideapps.com/u/gvalero)\
**Post date:** [July 20, 2023, 3:13pm UTC](https://community.glideapps.com/t/how-does-converting-unit-works/64172/2 "2023-07-20T15:13:43Z")

</div>

Hola!

Using this code from [Assemblysys.com](https://assemblysys.com/convert-a-decimal-number-into-a-fraction/), the solution in Glide would be:

 ![image](https://us1.discourse-cdn.com/flex002/uploads/glideapps/original/3X/3/f/3f0f7eb56d90ecbdb03885ac7ccfa37d4cf0846f.png)

What you have to do is to use this JS code:

 ![image](https://us1.discourse-cdn.com/flex002/uploads/glideapps/original/3X/8/9/89b545e4621b2b4633a1bf4aa40792d0df861132.png)

 ![image](https://us1.discourse-cdn.com/flex002/uploads/glideapps/original/3X/e/3/e3e7d0ebb6b14ea4bab04d8ad11723ca1397b0a0.png)

```auto
function decimalToFraction(value, donly = true) {
  var tolerance = 1.0E-6; // from how many decimals the number is rounded
  var h1 = 1;
  var h2 = 0;
  var k1 = 0;
  var k2 = 1;
  var negative = false;
  var i;

  if (parseInt(value) == value) { // if value is an integer, stop the script
    return value;
  } else if (value < 0) {
    negative = true;
    value = -value;
  }

  if (donly) {
    i = parseInt(value);
    value -= i;
  }

  var b = value;

  do {
    var a = Math.floor(b);
    console.log(a)
    var aux = h1;
    h1 = a * h1 + h2;
    h2 = aux;
    aux = k1;
    k1 = a * k1 + k2;
    k2 = aux;
    b = 1 / (b - a);
  } while (Math.abs(value - h1 / k1) > value * tolerance);

  return (negative ? "-" : '') + ((donly & (i != 0)) ? i + ' ' : '') + (h1 == 0 ? '' : h1 + "/" + k1);
}

return decimalToFraction(p1) + " " + p2

```

I hope it helps you!

Saludos!

---

<div class="post-metadata">

**Author:** ![Trustin](https://sea2.discourse-cdn.com/flex002/user_avatar/community.glideapps.com/trustin/32/88217_2.png) [@Trustin](https://community.glideapps.com/u/Trustin)\
**Post date:** [July 21, 2023, 12:02am UTC](https://community.glideapps.com/t/how-does-converting-unit-works/64172/3 "2023-07-21T00:02:08Z")

</div>

Woah thanks for the quality reply!  
I was trying to figure out Glide’s Convert Unit Computed Column but yeah JavaScript does the work too!
