Script to convert a decimal number to its fractional form

**1.**

__-------------------------------------------------------------------------------------------------------------------------------__

__------------------__

**HOW THIS SCRIPT WORKS:***In this script the decimal number is converted to fractional form by multiplyng and dividing it with necessary multipe of 10, for example 23.48 is converted to 2348/100 . Then HCF of 2348 and 100 is calculatted and both numerator and denominator are divided by HCF to get the result.*

-------------------------------------------------------------------------------------------------------------------------------------------------

-------------------------------------------------------------------------------------------

**#!/bin/bash**

#to convert the given decimal number to fraction

#for converting decimal to fraction

#converting to fractional form

**#checking if denominator or numerator is greater**

num=$(echo "$1" | cut -d . -f 1)

den=$(echo "$1" | cut -d . -f 2)

num="$num$den"

zero=$(echo $den | wc -c)

den=$((10**(zero-1)))

num=$(echo "$1" | cut -d . -f 1)

den=$(echo "$1" | cut -d . -f 2)

num="$num$den"

zero=$(echo $den | wc -c)

den=$((10**(zero-1)))

**#finding hcf**

if [[ $num -gt $den ]];then

greater=$num

lower=$den

else

greater=$den

lower=$num

fi

if [[ $num -gt $den ]];then

greater=$num

lower=$den

else

greater=$den

lower=$num

fi

**#dividing numerator and denominator by hcf**

while [ $lower -ne 0 ];do

hcf=$lower

lower=$((greater%lower))

greater=$hcf

done

while [ $lower -ne 0 ];do

hcf=$lower

lower=$((greater%lower))

greater=$hcf

done

**#answer**

num=$((num/hcf))

den=$((den/hcf))

num=$((num/hcf))

den=$((den/hcf))

echo "= $num/$den"

echo "= $num/$den"

-------------------------------------------------------------------------------------------

In my case the script is named

**. To find fractional form of any decimal number , say**

*fraction**6.24 , type*

**and just press enter ...**

*fraction 6.24***..abra-ca-dabra..**

**things I noticed about this script :*** even with too many digits after decimal place , instant result is obtained.

**2.**

__-------------------------------------------------------------------------------------------------------------------------------__

__------------------__

**HOW THIS SCRIPT WORKS:***In this script the decimal number is multiplied by natural number from 2 onwards and checked whether an integer is obtained.The fractional form of the decimal number will be integer divided by natural number (which when multiplied with the decimal number made it an integer).*

-------------------------------------------------------------------------------------------------------------------------------------------------

-------------------------------------------------------------------------------------------

**#!/bin/bash**

#to convert the given decimal number to fraction

**for (( i=1; i>0; i++ ));do**

**check=$(echo $(echo "$1*$i" | bc | cut -d . -f 2)*1 | bc)**

**if [[ $check == 0 ]];then**

**echo -e "=\e[1;34m$(echo "$1*$i" | bc | cut -d . -f 1)\e[0m/\e[1;34m$i\e[0m"**

**exit 0**

**fi**

**done**

**-------------------------------------------------------------------------------------------**

**things I noticed about this script :*** with too many digit after decimal place , result takes very long time.

*Enjoy Linux !!*

*Don't know what to do with these codes ?? click here*
## No comments:

## Post a Comment