Decimal2Fraction

Extension published on January 21, 2022

🏷️ Tags: #extensions

A non-visible extension that converts a decimal to a fraction. There is a way to replicate this with blocks, however a floating error will occur due to some minor problems of App Inventor.

Made with Niotron IDE at https://ide.niotron.com.

Current version: 2 Package name: com.gordonlu.decimal2fraction.aix

πŸ“– Documentation

DecimalToFraction - Converts a decimal to a fraction. Returns: text

Parameters: number = number (double), separator = text

GetDenominator - Returns the denominator of the fraction from the decimal. Returns: number (long)

Parameters: number = number (double)

GetNumerator - Returns the numerator of the fraction from the decimal. Returns: number (long)

Parameters: number = number (double)

Open Source

package com.gordonlu.decimal2fraction;


import android.app.Activity;

import android.content.Context;

import com.google.appinventor.components.annotations.*;

import com.google.appinventor.components.common.ComponentCategory;

import com.google.appinventor.components.runtime.AndroidNonvisibleComponent;

import com.google.appinventor.components.runtime.ComponentContainer;

import com.google.appinventor.components.runtime.EventDispatcher;

import java.util.*;


@DesignerComponent(

version = 1,

description = "Converts a decimal into a fraction.",

category = ComponentCategory.EXTENSION,

nonVisible = true,

iconName = "https://docs.google.com/drawings/d/e/2PACX-1vQCI87PHLBF0jb8QWyYmIRQSjjNW3EFXf-qpsWCvBYkUQ9vEgPAB8SpxcMpblxNpbIYrjCjLrRLIU2c/pub?w=16&h=16")


@SimpleObject(external = true)

//Libraries

@UsesLibraries(libraries = "")

//Permissions

@UsesPermissions(permissionNames = "")


public class Decimal2Fraction extends AndroidNonvisibleComponent {


//Activity and Context

private Context context;

private Activity activity;


public Decimal2Fraction(ComponentContainer container){

super(container.$form());

this.activity = container.$context();

this.context = container.$context();

}


@SimpleFunction(description = "Turns a decimal into a fraction.")

public static String DecimalToFraction(double number, String separator) {

double intVal = Math.floor(number);

double fVal = number - intVal;

final long pVal = 1000000000;

long gcdVal = gcd(Math.round(fVal * pVal), pVal);


long num = Math.round(fVal * pVal) / gcdVal;

long deno = pVal / gcdVal;

return ((long)(intVal * deno) +

num + separator + deno);

}

@SimpleFunction(description = "Gets the numerator from the decimal.")

public static long GetNumerator(double number) {

double intVal = Math.floor(number);

double fVal = number - intVal;

final long pVal = 1000000000;

long gcdVal = gcd(Math.round(fVal * pVal), pVal);


long num = Math.round(fVal * pVal) / gcdVal;

long deno = pVal / gcdVal;

return ((long)(intVal * deno) +

num);

}

@SimpleFunction(description = "Gets the denominator from the decimal.")

public static long GetDenominator(double number) {

double intVal = Math.floor(number);

double fVal = number - intVal;

final long pVal = 1000000000;

long gcdVal = gcd(Math.round(fVal * pVal), pVal);


long num = Math.round(fVal * pVal) / gcdVal;

long deno = pVal / gcdVal;

return ((long)deno);

}

private static long gcd(long a, long b)

{

if (a == 0)

return b;

else if (b == 0)

return a;

if (a < b)

return gcd(a, b % a);

else

return gcd(b, a % b);

}

}

TERMS AND CONDITIONS

By downloading or sharing my extension, you agree to follow these terms and conditions.

  1. Have fun using the extension!

  2. This, along with the MIT App Inventor original post above, are the only authorized places to download this extension. As an organization, you are not allowed to host and advertize this extension on your website, document, tweet, blog, article or any software you made without my authorization. You are also reminded not to include a direct download link of this extension on your website without my permission. If you are interested in the actions listed above, please contact me via email. Do remember that if you violate term no. 2, AICODE has the right to flag your website, document, tweet, blog or article as a violation of copyright, or other legal actions will be taken. Please contact me if you find anyone do this.

However, you can copy the link of this webpage and share it to someone as a personal identity (not as an organization), or sharing it on these authorized forums: App Inventor, Kodular, Niotron, Appzard and Android Builder communities. The term organization includes schools, markets and app stores, companies and social groups. Thank you for your understanding.

Those organizations are authorized to advertize or host my extension:

  • Kodular admins

  • MIT App Inventor admins

  • Pura Vida Apps / Taifun Baer

  1. Selling this extension/product to anyone is not allowed unless authorized by AICODE.

  2. The extension is made Open Source. You can modify the code, but you should give credit by providing a link to this site.

  3. These terms and conditions are written and issued on December 20, 2021. AICODE has the total permission to edit these terms and conditions anytime.

πŸ’¬ Messages

βš™οΈ πŸ”’ CLOSED 02.19.2022 - This extension will no longer receive any updates.

βš™οΈ πŸ”“ OPENED 02.20.2022 - This extension may receive updates in the future.