.

Labels

.

Convert decimal to fraction


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

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)))
#checking if denominator or numerator is greater
if [[ $num -gt $den ]];then
   greater=$num
   lower=$den
else
   greater=$den
   lower=$num
fi
#finding hcf
while [ $lower -ne 0 ];do
    hcf=$lower   
    lower=$((greater%lower))
    greater=$hcf
done
#dividing numerator and denominator by hcf
num=$((num/hcf))
den=$((den/hcf))
#answer
echo "= $num/$den"


-------------------------------------------------------------------------------------------
In my case the script is named fraction. To find fractional form of any decimal number , say 6.24 , type fraction 6.24 and just press enter ...

..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