Sunday, 13 September 2015

DECIMAL TO FRACTION CODE



-----------------------------------------------------------------------------------------------------------------
COPY AND PASTE BELOW CODE IN POST HTML SECTION
------------------------------------------------------------------------------------------------------------------


<html>
<head>

<style type="text/css">
input { font-size: 1em; }
@media (max-width:799px) {
.btn { font-size: 1.2em; padding:7px; }
}
@media (min-width:800px) {
.btn { font-size: 1.2em; padding:4px; }
}
.intext {
background:url(data:image/gif;base64,R0lGODlhsQBfALMAAO+9jPe9iP+9e/+9hOLBnOnBnMbGwdbGs63G3rXG1r3GzrnG2qXG563G56XG75zG9ywAAAAAsQBfAAAE/lDISau9OOvNZQBgKI5BZ55oqq7s+QHEcRi0gtyMo+98z9wIBc0gI4AGraRyyex8YjbE7kGtWq/YbFb3SygORmRzTC5bPjObQ8tuu9+PnzBsrttPsIMazu/7q3JgAHeEdzAGCAx/i4x+DgwJBgQlhZUsATEJio2cnX0MXwCUlqQYAXoNa56rrG8OCJKjpZaYBg2tuLlvsAWzhQAHUrrDxFsJB4O+Y6cKqsXP0FSgBGLKl8HR2doPCdTWKcAI2+PZx7LfGAAGm+Tt0AgH5+gS6uzu98/w8sqn9vj/xbrNI5AAoMFoDhQkmxXAgLODEIkxODCrQMGIGJ8ptHTAX8aP/rgQECDU8CHIk60cUKxjEaXLYRvJdHxJM5fIMQ5r6ky5UomBnUBZqfQZtKinoSx+Gl3ayMFIFQdMMp0Kp8HTE1GpavVjFavUrWC1NOjFAYDHsGixJNhXQUHat216YjgAt24WBmQvmLXL18paDG77Cn4gdwKBwYMZLJxwEXHfwgC+Ok6LQJbSyXydTgggDnNfA4Y9C25A6bJou08Dn7a7svPquqAFuH79VoEAzrTrIhAQOTdcBh8k+57qAITw4UsV90YeFsGAvczBJpAwO/rU2KatM316WPtUxfTOetc5fUL18TtjS8iOvqaDvLzFtz9puwL7+SffW4CO32V9C/f1/heRfnrJJyBA/11A14EZ4bVBgAy6g5QGATQW4T/qbVDALRfiowBbFxBwXIfFILBYBwuSuI1mKkCoYi4TQjXii510lURWNOpioxIEcJgjKwnAx6OPP3KSwIlLtFRkIwaAmAQAqi3JhwMZ1kHAeVJmEaQl6sz440ROmkFQlljE5Es/ZD5w0zwFRJkjlUh+MyaNCQk5jwQDENBMh3XeqaECBloHip1+poPIfLDEWSiFenqJmQPdhLmoBgXYMlwDBhA6qQqYAHraNJJumkItqQyGqRGikkSAAZq8BWksqc5ySCLEBYFMNbFac4gmjv7zSCTIhJqrrAVAkUAivUqEgBeSLRSg6LC55pEGDsnC8cgNkRCBKrTcqgBAsTIMcSwQ5JaL7RDaOtvtuuy2O0EEAAA7);
font-family:'Comic Sans MS';
font-weight:bold;
}
.outtext {
background:url(data:image/gif;base64,R0lGODlhsQBfALMAAO+9jPe9iP+9e/+9hOLBnOnBnMbGwdbGs63G3rXG1r3GzrnG2qXG563G56XG75zG9ywAAAAAsQBfAAAE/lDISau9OOvNZQBgKI5BZ55oqq7s+QHEcRi0gtyMo+98z9wIBc0gI4AGraRyyex8YjbE7kGtWq/YbFb3SygORmRzTC5bPjObQ8tuu9+PnzBsrttPsIMazu/7q3JgAHeEdzAGCAx/i4x+DgwJBgQlhZUsATEJio2cnX0MXwCUlqQYAXoNa56rrG8OCJKjpZaYBg2tuLlvsAWzhQAHUrrDxFsJB4O+Y6cKqsXP0FSgBGLKl8HR2doPCdTWKcAI2+PZx7LfGAAGm+Tt0AgH5+gS6uzu98/w8sqn9vj/xbrNI5AAoMFoDhQkmxXAgLODEIkxODCrQMGIGJ8ptHTAX8aP/rgQECDU8CHIk60cUKxjEaXLYRvJdHxJM5fIMQ5r6ky5UomBnUBZqfQZtKinoSx+Gl3ayMFIFQdMMp0Kp8HTE1GpavVjFavUrWC1NOjFAYDHsGixJNhXQUHat216YjgAt24WBmQvmLXL18paDG77Cn4gdwKBwYMZLJxwEXHfwgC+Ok6LQJbSyXydTgggDnNfA4Y9C25A6bJou08Dn7a7svPquqAFuH79VoEAzrTrIhAQOTdcBh8k+57qAITw4UsV90YeFsGAvczBJpAwO/rU2KatM316WPtUxfTOetc5fUL18TtjS8iOvqaDvLzFtz9puwL7+SffW4CO32V9C/f1/heRfnrJJyBA/11A14EZ4bVBgAy6g5QGATQW4T/qbVDALRfiowBbFxBwXIfFILBYBwuSuI1mKkCoYi4TQjXii510lURWNOpioxIEcJgjKwnAx6OPP3KSwIlLtFRkIwaAmAQAqi3JhwMZ1kHAeVJmEaQl6sz440ROmkFQlljE5Es/ZD5w0zwFRJkjlUh+MyaNCQk5jwQDENBMh3XeqaECBloHip1+poPIfLDEWSiFenqJmQPdhLmoBgXYMlwDBhA6qQqYAHraNJJumkItqQyGqRGikkSAAZq8BWksqc5ySCLEBYFMNbFac4gmjv7zSCTIhJqrrAVAkUAivUqEgBeSLRSg6LC55pEGDsnC8cgNkRCBKrTcqgBAsTIMcSwQ5JaL7RDaOtvtuuy2O0EEAAA7);
font-family:'Comic Sans MS';
font-weight:bold;
}
a:link { text-decoration: none; color: #083d8d; }
a:visited { text-decoration: none; color: #083d8d; }
a:active { text-decoration: underline; color: #000000; }
a:hover { text-decoration: underline; color: #000000; }
a:focus { outline: none; }

.math { font-family: "Times New Roman"; font-size: 1.6em; padding-left: 6px; font-weight: normal;}
.mathsymbol { font-family: "Times New Roman"; font-size: 1.2em; font-weight: normal; }
.text { font-family: "Times New Roman", Times, serif; font-size: 1.2em }
.code { font-family: "Courier New"; font-size: 1.0em; padding-left: 6px;}
.codebg { font-family: "Courier New"; font-size: 1.0em; color: #000000; background:#f0f0f0; margin-top: 0px; margin-bottom: 0px; padding: 10px; }
.codebg.p { font-family: "Courier New"; font-size: 1.0em; color: #000000; background:#f0f0f0; margin-top: 0px; margin-bottom: 0px;}
p.code { font-family: "Courier New"; font-size: 1.0em; color: #000000; background:#f0f0f0; margin:0px; padding:5px; border-width:1px; border-color:#ccc; border-style:solid; line-height:20px}
p.wcode { font-family: "Courier New"; font-size: 1.0em; color: #000000}
p.tcode { font-family: "Courier New"; font-size: 1.0em; color: #000000; background:#f0f0f0; margin:0px; padding:5px; border-width:1px; border-color:#ccc; border-style:solid; line-height:20px }

.table1 .hov { border: 2px black solid; padding: 4px; }
.table1 .ho2 { border: 2px #707070 solid; padding: 4px; }
.table1 .td { border: 1px black solid; padding: 5px; }

table.dtable, table.ntable { border: 1px #ccc solid; border-collapse:collapse; padding: 5px;}
table.dtable th, table.dtable td, table.ntable th, table.ntable td { border: 1px #ccc solid; border-collapse:collapse; padding: 5px; font-weight:normal;}
table.dtable th, table.ntable th { text-align:center }
table.ntable td { text-align:right }

table.calc, .calc2 {
border-spacing: 0px;
border-collapse: separate;
background: #e8e8e8;
border:1px #ddd solid;
border-radius:7px;
-moz-border-radius:7px;
-webkit-border-radius:7px;
}
table.calc td, .calc2 td {
padding: 2px;
border-width: 0px;
border-style: none;
background: #e8e8e8;
}
.calc2 select { padding:4px 2px; font-size:1.2em; font-family:Arial, Helvetica, sans-serif }
@media all and (max-width: 576px) {
.calc2 { padding:8px; margin-left:auto; margin-right:auto; }
.calc2 td { float:left; }
.calc2 td:nth-child(2) { clear:left; }
.calc2 tr td:nth-child(3) { padding-top:10px; }
.calc2 input[type=text], .calc2 input[type=number], .calc2 input[type=url] { width:190px; font-size:x-large; padding-left:3px; }
.calc2 tr td:nth-child(3) select { margin-top:-8px; }
}
@media all and (min-width: 577px) {
.calc2 { padding:15px; }
.calc2 tr { line-height:40px; }
.calc2 input[type=text], .calc2 input[type=number], .calc2 input[type=url] { width:220px; font-size:x-large; padding-left:3px; }
}

#area {
  font-size:x-large;
  width:100%;
}
</style>

<script type='text/javascript'>
//<![CDATA[
function setfocus() {
document.calcform.x.focus();
}
function calc() {
x = document.calcform.x.value;
y = convert(x);
y = roundresult(y);
document.calcform.y.value = y;
}
function calctest() {
x = document.calcform.x.value;
y = convert(x);
//y = roundresult(y);
y = roundresult1(y);
document.calcform.y.value = y;
}
function calc3() {
x1 = document.calcform.x1.value;
x2 = document.calcform.x2.value;
y = convert(x1,x2);
y = roundresult(y);
document.calcform.y.value = y;
}
function calc4() {
x1 = document.calcform.x1.value;
x2 = document.calcform.x2.value;
x3 = document.calcform.x3.value;
y = convert(x1,x2,x3);
y = roundresult(y);
//yy = y.toString();
//if( yy.length>12 ) {
// y = parseFloat(y);
// y = y.toPrecision(10);
//}
document.calcform.y.value = y;
}
function calc5() {
x = document.calcform.x.value;
y = convert1(x);
y = roundresult(y);
document.calcform.y1.value = y;
y = convert2(x);
y = roundresult(y);
document.calcform.y2.value = y;
}
function calcbase(b1,b2) {
x = document.calcform.x.value;
document.calcform.y.value = convertbase(x,b1,b2);
}
function calcbase2() {
x = document.calcform.x.value;
y = convert(x);
document.calcform.y.value = y;
}

function roundresult(x) {
y = parseFloat(x);
y = roundnum(y,10);
return y;
}
function roundnum(x,p) {
var i;
var n=parseFloat(x);
var m=n.toPrecision(p+1);
var y=String(m);
i=y.indexOf('e');
if( i==-1 ) i=y.length;
j=y.indexOf('.');
if( i>j && j!=-1 )
{
while(i>0)
{
if(y.charAt(--i)=='0')
y = removeAt(y,i);
else
break;
}
if(y.charAt(i)=='.')
y = removeAt(y,i);
}
return y;
}
function removeAt(s,i) {
s = s.substring(0,i)+s.substring(i+1,s.length);
return s;
}

function convertbase(x,b1,b2) {
d="0123456789ABCDEFX";
x=x.replace(/ /,"");
x=x.toUpperCase();
y=0;
// return b1+b2;
for(i=x.length, s=1; i>=1; i--,s*=b1) {
c = x.charAt(i-1);
for(j=0; j<b1+1; j++) {
if( j==b1 ) {
y = "bad input number";
return y;
}
if( c == d.charAt(j) )
break;
}
y+=j*s;
}
x=y;
if( x==0 ) return "0";
y="";
for(s=1; s<=x; s*=b2);
if( x!=s ) s/=b2;
for(p=s; p>=1; p/=b2) {
i = Math.floor(x/p);
y+= d.charAt(i);
x-= i*p;
}
return y;
}
function convertbase2(x,b1,b2) {
x = parseInt(x, b1);
y = x.toString(b2);
return y;
}
var gcd = function(a, b) {
if ( ! b) {
return a;
}
return gcd(b, a % b);
};

function digits_after_period(x)
{
f = x.toString();
i = f.indexOf('.');
len = f.length-i-1;
return len;
}

function parseFraction(s)
{
var n1=n2=0;
var sign=n3=1;
i = s.indexOf(' ');
if( i==0 ) s=s.substring(1);
i = s.indexOf('-');
if( i==0 ) {s=s.substring(1); sign=-1;}
i = s.indexOf(' ');
if( i==0 ) s=s.substring(1);
i = s.indexOf(' ');
if( !isNaN(parseInt(s)) && isFinite(s) ) i=s.length;
if( i!=-1 )
{
n1 = parseInt(s.substring(0,i));
s = s.substring(i+1);
}
i = s.indexOf('/');
if( i!=-1 )
{
n2 = parseInt(s.substring(0,i));
s = s.substring(i+1);
n3 = parseInt(s);
}
return [sign,n1,n2,n3];
}

//]]>
</script>

</head>

<body>

<section id="doc">
<!-- #BeginEditable "doctext" -->
<h1>Decimal to Fraction Conversion</h1>
<ul>
<li><a href="#calculator">Decimal to fraction conversion calculator</a></li>
<li><a href="#how">How to convert decimal to fraction</a></li>
<li><a href="#table">Decimal to fraction conversion table</a></li>
</ul>

<h2><a name="calculator"></a>Decimal to fraction conversion calculator</h2>
<p>Enter a decimal number and press the <i>Convert</i> button:</p>

<form name="calcform">
<table class="calc">
<tr>
<td><label for="x">Enter decimal number:</label></td>
</tr>
<tr>
<td class="math"><input type="number" id="x" name="x" class="intext" autofocus tabindex="1"></td>
</tr>
<tr>
<td><input onclick="convert()" type="button" value="Convert" class="btn" tabindex="3"> <input type="reset" value="Reset" class="btn" tabindex="4"></td>
</tr>
<!--
<tr>
<td>Greatest Common Divisor (GCD):</td>
<td class="math"><input name="g" class="outtext" readonly tabindex="5"></td>
</tr>
<tr>
<td>Fraction result:</td>
<td class="math"><input name="y" class="outtext" readonly tabindex="6"></td>
</tr>
-->
<tr>
<td>Fraction result:</td>
</tr>
<tr>
<td>
<table id="tbl1">
<tr>
<td id="td11" rowspan="2"><input type="text" id="y" tabindex="1" readonly></td>
<td id="td12"><input type="text" id="n" tabindex="2" readonly></td>
</tr>
<tr>
<td id="td21"><input type="text" id="d" tabindex="3" readonly></td>
</tr>
</table>
</td>
</tr>
<tr>
<td>Calculation:</td>
</tr>
<tr>
<td><textarea rows="6" id="area" tabindex="5" class="outtext" readonly></textarea></td>
</tr>
</table>
</form>
<h2><a name="how"></a>How to convert decimal to fraction</h2>
<p>Example: convert 2.56 to fraction:</p>
<p class="math">2.56 = 2+56/100</p>
<p class="math">gcd(56,100) = 4</p>
<p class="math">2+56/100 = 2+(56/4)/(100/4) = 2+14/25</p>
<p>&nbsp;</p>
<ol>
<li>Count the number of digits (d) to the right of the decimal point of the decimal number x.<p>Example: 2.56 has 2 digits to the right of the decimal point, so d=2.</li>
<li>Calculate the factor (f) for making the decimal number an integer:<p class="mathsymbol"><i>f</i> = 10<i><sup>d</sup></i></p>
<p>Example:</p>
<p class="mathsymbol"><i>f</i> = 10<sup>2</sup> = 100</li>
<li>Multiply and divide the decimal number x by the factor f:<p class="mathsymbol"><i>x</i> &times; <i>f</i> / <i>f</i>&nbsp; = <i>&nbsp;y</i> / <i>f</i></p>
<p>Example:</p>
<p class="mathsymbol">2.56 &times; 100 / 100 = 256 / 100</li>
<p>Example:</p><p class="mathsymbol">gcd(256,100) = 4</li>
<li>Reduce the fraction by dividing the numerator and denominator by the gcd value:<p>Example:</p>
<p class="mathsymbol">256 / 100 = (256/4) / (100/4) = 64/25</p></li>
</ol>
<h2><a name="table"></a>How to convert repeating decimal to fraction</h2>
<h4>Example #1</h4>
<p>Convert 0.333333... to fraction:</p>
<p class="math"><em>x</em> = 0.333333...</p>
<p class="math">10<em>x</em> = 3.333333...</p>
<p class="math">10<em>x</em> - <em>x</em> = 9<em>x</em> = 3</p>
<p class="math"><em>x</em> = 3/9 = 1/3</p>
<h4>Example #2</h4>
<p>Convert 0.0565656... to fraction:</p>
<p class="math"><em>x</em> = 0.0565656...</p>
<p class="math">100<em>x</em> = 5.6565656...</p>
<p class="math">100<em>x</em> - <em>x</em> = 99<em>x</em> = 5.6</p>
<p class="math">990<em>x</em> = 56</p>
<p class="math"><em>x</em> = 56/990 = 28/495</p>
<h2>Decimal to fraction conversion table</h2>
<table class="ntable">
<tr>
<th>Decimal</th>
<th>Fraction</th>
</tr>
<tr>
<td>0.01000000</td>
<td>1/100</td>
</tr>
<tr>
<td>0.10000000</td>
<td>1/10</td>
</tr>
<tr>
<td>0.11111111</td>
<td>1/9</td>
</tr>
<tr>
<td>0.12500000</td>
<td>1/8</td>
</tr>
<tr>
<td>0.14285714</td>
<td>1/7</td>
</tr>
<tr>
<td>0.16666667</td>
<td>1/6</td>
</tr>
<tr>
<td>0.20000000</td>
<td>1/5</td>
</tr>
<tr>
<td>0.22222222</td>
<td>2/9</td>
</tr>
<tr>
<td>0.25000000</td>
<td>1/4</td>
</tr>
<tr>
<td>0.28571429</td>
<td>2/7</td>
</tr>
<tr>
<td>0.30000000</td>
<td>3/10</td>
</tr>
<tr>
<td>0.33333333</td>
<td>1/3</td>
</tr>
<tr>
<td>0.37500000</td>
<td>3/8</td>
</tr>
<tr>
<td>0.40000000</td>
<td>2/5</td>
</tr>
<tr>
<td>0.42857143</td>
<td>3/7</td>
</tr>
<tr>
<td>0.44444444</td>
<td>4/9</td>
</tr>
<tr>
<td>0.50000000</td>
<td>1/2</td>
</tr>
<tr>
<td>0.55555555</td>
<td>5/9</td>
</tr>
<tr>
<td>0.57142858</td>
<td>4/7</td>
</tr>
<tr>
<td>0.62500000</td>
<td>5/8</td>
</tr>
<tr>
<td>0.66666667</td>
<td>2/3</td>
</tr>
<tr>
<td>0.60000000</td>
<td>3/5</td>
</tr>
<tr>
<td>0.70000000</td>
<td>7/10</td>
</tr>
<tr>
<td>0.71428571</td>
<td>5/7</td>
</tr>
<tr>
<td>0.75000000</td>
<td>3/4</td>
</tr>
<tr>
<td>0.77777778</td>
<td>7/9</td>
</tr>
<tr>
<td>0.80000000</td>
<td>4/5</td>
</tr>
<tr>
<td>0.83333333</td>
<td>5/6</td>
</tr>
<tr>
<td>0.85714286</td>
<td>6/7</td>
</tr>
<tr>
<td>0.87500000</td>
<td>7/8</td>
</tr>
<tr>
<td>0.88888889</td>
<td>8/9</td>
</tr>
<tr>
<td>0.90000000</td>
<td>9/10</td>
</tr>
</table>
<p>&nbsp;</p>

<p>&nbsp;</p>
<hr>

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.4/jquery.min.js"></script>

<script type="text/javascript">
<!--
var gcd2 = function(a, b, f) {
if( f )
{
if ( b<=1 )
return a;
}
else
{
if ( !b )
return a;
}
return gcd2(b, a % b, f);
};
function convert()
{
var sign = '';
var sign2 = '+';
x = $('#x').val();
x2 = parseFloat(x);
var absx=Math.abs(x2);
var y=Math.floor(absx);
var frac=roundresult(absx-y);
if( x2<0 ) sign = sign2 = '-';
d = digits_after_period(absx);
den = Math.round(Math.pow(10,d));
num = Math.round(frac*den);
var len=num.toString().length;
var f=false;
if( len>8 ) f=true;
g = gcd2(num,den,f);
num2 = Math.round(num/g);
den2 = Math.round(den/g);
var txt=x+"\n";
if( absx>1 ) {
$('#td11').show();
txt+="= "+sign+y+sign2+frac+"\n";
txt+="= "+sign+y+sign2;
}
else {
$('#td11').hide();
txt+="= "+sign;
}
txt+=num+"/"+den+"\n";
if( g>1 ) {
txt+="( gcd("+num+","+den+") = "+g+" )\n";
txt+="= "+sign;
if( absx>1 )
txt+=y+sign2;
txt+=num2+"/"+den2+"\n";
}

if( y>0 )
txt+="= "+sign+(num2+den2*y)+"/"+den2;
$('#y').val(y);
$('#n').val(num2);
$('#d').val(den2);
$('#area').val(txt);
}
//-->
</script>
<!-- #EndEditable -->
</section>



</body>
</html>







0 comments:

Post a Comment

:) :)) ;(( :-) =)) ;( ;-( :d :-d @-) :p :o :>) (o) [-( :-? (p) :-s (m) 8-) :-t :-b b-( :-# =p~ $-) (b) (f) x-) (k) (h) (c) cheer
Click to see the code!
To insert emoticon you must added at least one space before the code.

FB Gadgets | Template Designed by Fatakat PhotosCoolBThemes.com
Code by : paid web directory

https://www.google.co.uk/search?q=site%3Ablogspot.com+fbgadgets