# Convert Numbers into fractions

**URL:** <https://community.glideapps.com/t/convert-numbers-into-fractions/73956>\
**Category:** Ask for Help\
**Created:** [June 7, 2024, 2:15pm UTC](https://community.glideapps.com/t/convert-numbers-into-fractions/73956 "2024-06-07T14:15:38Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Denis1](https://sea2.discourse-cdn.com/flex002/user_avatar/community.glideapps.com/denis1/32/65827_2.png) [@Denis1](https://community.glideapps.com/u/Denis1)\
**Post date:** [June 7, 2024, 2:15pm UTC](https://community.glideapps.com/t/convert-numbers-into-fractions/73956/1 "2024-06-07T14:15:38Z")

</div>

Is there a way to convert numbers with decimals into fractions

---

<div class="post-metadata">

**Author:** ![Jeff\_Hager](https://sea2.discourse-cdn.com/flex002/user_avatar/community.glideapps.com/jeff_hager/32/43_2.png) [@Jeff\_Hager](https://community.glideapps.com/u/Jeff_Hager)\
**Post date:** [June 7, 2024, 3:58pm UTC](https://community.glideapps.com/t/convert-numbers-into-fractions/73956/2 "2024-06-07T15:58:20Z")

</div>

How would you view the following:

- Would 2 result in 2 or 2/1
- Would 2.5 result in 2 1/2 or 5/2

---

<div class="post-metadata">

**Author:** ![Denis1](https://sea2.discourse-cdn.com/flex002/user_avatar/community.glideapps.com/denis1/32/65827_2.png) [@Denis1](https://community.glideapps.com/u/Denis1)\
**Post date:** [June 7, 2024, 8:48pm UTC](https://community.glideapps.com/t/convert-numbers-into-fractions/73956/3 "2024-06-07T20:48:32Z")

</div>

I would like it to show the decimal side as a fraction for instance 2.25 to 2¼

---

<div class="post-metadata">

**Author:** ![ThinhDinh](https://sea2.discourse-cdn.com/flex002/user_avatar/community.glideapps.com/thinhdinh/32/49_2.png) [@ThinhDinh](https://community.glideapps.com/u/ThinhDinh)\
**Post date:** [June 8, 2024, 12:28am UTC](https://community.glideapps.com/t/convert-numbers-into-fractions/73956/4 "2024-06-08T00:28:10Z")

</div>

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

Kept this screenshot for you, Jeff and @Darren_Murphy because I just remembered how you all taught me about how decimals don’t really look the same way they are. What is it called? Floating-point precision or something?

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

Anyway, the code was adjusted and it should work now. Please use this in your JavaScript column.

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

```auto
// Separate the integer part and the decimal part
const integerPart = Math.floor(p1);
const decimalPart = p1 - integerPart;

// If there is no decimal part, return the integer part
if (decimalPart === 0) {
    return `${integerPart}`;
}

// Convert decimal part to fraction
const gcd = (a, b) => b ? gcd(b, a % b) : a;
const decimalString = decimalPart.toFixed(10); // Fixing precision issues
const denominator = Math.pow(10, decimalString.split('.')[1].length);
const numerator = Math.round(decimalPart * denominator);
const commonDivisor = gcd(numerator, denominator);

const simplifiedNumerator = numerator / commonDivisor;
const simplifiedDenominator = denominator / commonDivisor;

// Return the mixed fraction
return `${integerPart} ${simplifiedNumerator}/${simplifiedDenominator}`;

```

---

<div class="post-metadata">

**Author:** ![Denis1](https://sea2.discourse-cdn.com/flex002/user_avatar/community.glideapps.com/denis1/32/65827_2.png) [@Denis1](https://community.glideapps.com/u/Denis1)\
**Post date:** [June 8, 2024, 2:49am UTC](https://community.glideapps.com/t/convert-numbers-into-fractions/73956/5 "2024-06-08T02:49:48Z")

</div>

It worked but I think there is something that need to be adjusted too. When the number with decimal has a zero like this 0.75 it’s having a zero too on the faction side too like this 0¾ instead of just the ¾. Is there a way ho adjust that sir

---

<div class="post-metadata">

**Author:** ![Denis1](https://sea2.discourse-cdn.com/flex002/user_avatar/community.glideapps.com/denis1/32/65827_2.png) [@Denis1](https://community.glideapps.com/u/Denis1)\
**Post date:** [June 8, 2024, 3:18am UTC](https://community.glideapps.com/t/convert-numbers-into-fractions/73956/6 "2024-06-08T03:18:06Z")

</div>

I have managed to adjust it. But thanks for sharing this sir.  
// Separate the integer part and the decimal part  
const integerPart = Math.floor(p1);  
const decimalPart = p1 - integerPart;

// If there is no decimal part, return the integer part  
if (decimalPart === 0) {  
return `${integerPart} In stock`;  
}

// Convert decimal part to fraction  
const gcd = (a, b) =\> b ? gcd(b, a % b) : a;  
const decimalString = decimalPart.toFixed(10); // Fixing precision issues  
const denominator = Math.pow(10, decimalString.split(‘.’)[1].length);  
const numerator = Math.round(decimalPart \* denominator);  
const commonDivisor = gcd(numerator, denominator);

const simplifiedNumerator = numerator / commonDivisor;  
const simplifiedDenominator = denominator / commonDivisor;

// If there is no integer part, return just the fraction part with “Kg In stock”  
if (integerPart === 0) {  
return `${simplifiedNumerator}/${simplifiedDenominator} Kg In stock`;  
}

// Return the mixed fraction with appropriate unit  
return `${integerPart} ${simplifiedNumerator}/${simplifiedDenominator} Kg In stock`;

---

<div class="post-metadata">

**Author:** ![system](https://sea2.discourse-cdn.com/flex002/user_avatar/community.glideapps.com/system/32/53398_2.png) [@system](https://community.glideapps.com/u/system)\
**Post date:** [June 15, 2024, 3:18am UTC](https://community.glideapps.com/t/convert-numbers-into-fractions/73956/7 "2024-06-15T03:18:17Z")

</div>

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.
