Javascript code. The specific requirement is to convert any decimal number into a fraction.

Source: Internet
Author: User
<Script language = javascript>
<! --

Function DecimalToFraction (dec, digit ){
Dec = Math. round (dec * (Math. pow (10, digit)/Math. pow (10, digit );
Var tmpa, tmpb
Tmpa = dec x 100
Tmpb = 100
For (var I = 100; I> 0; I --){
Var tmp = (tmpa/I). toString ();
Var tmp2 = tmp. indexOf (".")
If (tmp2 =-1 ){
Var tmp3 = (tmpb/I). toString ();
Var tmp4 = tmp3.indexOf (".")
If (tmp4 =-1 ){
Tmpa = tmpa/I
Tmpb = tmpb/I
}
}
}
Alert (tmpa + "/" + tmpb)
}
DecimalToFraction (0.25, 2)
// -->
</SCRIPT>

//----------------------------------------------------------------------
----------------------------------------------------------------------
----------------------------------------------------------------------

<Script language = "JavaScript">
<! --
/**
Format Decimals
*/
Function FormatNumber (value, num ){
Var valuelen = String (value). length;
Var pointpos = String (value). indexOf (".");
Var decimalcount = 0;
If (pointpos =-1 ){
Decimalcount = 0;
} Else {
Decimalcount = valuelen-pointpos-1;
}
If (decimalcount <= num ){
Return value;
} Else if (decimalcount <= num + 1 ){
Value = NumberRound (value, num );
Alert (value );
Return value;
} Else {
Var templen = decimalcount-num-1;
Var endpos = valuelen-templen;
Var valuestr = String (value). substring (0, endpos );
Value = parseFloat (valuestr );
Value = NumberRound (value, num );
Return value;
}
}
/**
Rounding with precision
*/
Function NumberRound (value, num ){
Value = value * Math. pow (10, num );
Value = Math. round (value );
Value = value/Math. pow (10, num );
}

/**
Convert decimals to scores
*/
Function DecimalToFraction (dec, digit ){
Dec = FormatNumber (dec, digit );
Alert (dec );
Var decx, decy, decz;
Decy = decz = digit = Math. pow (10, digit );
Decx = dec * digit;
While (decx> 0 ){
Decz = decx;
Decx = decy % decz;
Decy = decz
}
Return (dec/decz + "/" + digit/decz );
}
Alert (DecimalToFraction (1.751, 2 ));
Alert (DecimalToFraction (1.745, 2 ));
// -->
</SCRIPT>

I use VBScript to implement JavaScript because of its defect in floating point calculation.

<Script language = "VBScript">
Function DecimalToFraction (dec)
If dec = 0 Then
DecimalToFraction = "0"
Exit Function
End If
If Fix (dec) = Dec Then
DecimalToFraction = "1"/CStr (dec)
Exit Function
End If
Dim fm: fm = 1/dec
If Fix (fm) = fm Then
DecimalToFraction = "1/" & CStr (fm)
Exit Function
End If
Dim nfm: nfm = fm
Dim I: I = 1
WHile True
Fz = I
Fm = nfm * I
If Fix (fm) = fm Then
DecimalToFraction = CStr (I) & "/" & CStr (fm)
Exit Function
End If
I = I + 1
If I> 32768 Then
DecimalToFraction = "overflow"
Exit Function
End If
Wend
End Function
MsgBox DecimalToFraction (3.14)
</Script>

First, convert the numerator to 1 and the denominator to any decimal number.
Then, when the numerator is always an integer, search up from 1.
At the same time, the denominator is multiplied by the same number until the denominator is converted into an integer, or the value is too large, leading to overflow.

Thank you. The problem has basically been solved. It is converted from the C # code. A lot of functions are used. I feel that JavaScript Functions are not rich enough and many functions need to be compiled by myself.
Thank you.
The Code is as follows:
<Script lang = "javascript">
String. prototype. trim = function ()
{
Return this. replace (/(^ \ s *) | (\ s * $)/g ,"");
}
// Rounding by precision
Function FormatNumber (B,)
{
Return Math. round (B * Math. pow (10, a)/Math. pow (10, );
}

Function DecimalToFraction (dec, _ digit)
{
Var returnF = String (dec );
Var NUMFRACS = 30;
Var numerator = new Array ();
Var denominator = New Array ();
Var IsMinus = false;
If (dec <0)
{
IsMinus = true;
}
Dec = Math. abs (dec );
Var pre = Math. floor (dec );

Var _ dec = FormatNumber (dec-pre, _ digit );

Var d = 0, d2 = 0, d3, d4 =-1;
Var maxNumerator, L2;

D = d2 = _ dec;

MaxNumerator = getMaxNumerator (d. toString (10 ));

Numerator [0] = 0; denominator [0] = 1;
Numerator [1] = 1; denominator [1] = 0;

For (var I = 2; I <NUMFRACS; I ++)
{
L2 = Math. round (d2 );
Numerator [I] = L2 * numerator [I-1] + numerator [I-2];
Denominator [I] = L2 * denominator [I-1] + denominator [I-2];
D3 = (numerator [I])/(denominator [I]);
If (d3 = d4)
{
Break;
}
If (Math. abs (numerator [I])> maxNumerator)
{
Break;
}

If (d3 = d)
{
Break;
}
D4 = d3;
D2 = 1/(d2-L2 );
}

For (var I = 0; I <NUMFRACS; I ++)
{
Var h = 0;
If (denominator [I]! = 0)
{
H = parseFloat (numerator [I])/parseFloat (denominator [I]);
}

If (denominator [I]! = 0 & FormatNumber (h, _ digit) = _ dec)
{
ReturnF = Math. abs (numerator [I]) + "/" + Math. abs (denominator [I]);
Break;
}
}

ReturnF = (IsMinus? "-": "") + (Pre = 0? "": Pre) + ("" + returnF );
Return returnF. trim ();
}

Function getMaxNumerator (f)
{
Var f2 = null;
Var ixe = f. indexOf ("E ");
If (ixe =-1) ixe = f. indexOf ("e ");
If (ixe =-1) f2 = f;
Else f2 = f. substr (0, ixe );

Var digits = null;
Var ix = f2.indexOf (".");
If (ix =-1)
{
Digits = f2;
}
Else if (ix = 0)
{
Digits = f2.substr (1, f2.length );
}
Else if (ix <f2.length)
{
Digits = f2.substr (0, ix) + f2.substr (ix + 1 );
}

Var L;
Try
{
L = parseInt (digits );
}
Catch (e)
{
Return 999999999;
}

Var numDigits = L. length;

Var L2 = f. length;
Var numIntDigits = L2.length;
If (L2 = 0) numIntDigits = 0;
Var numDigitsPastDecimal = numDigits-numIntDigits;

For (var I = numDigitsPastDecimal; I> 0 & L % 2 = 0; I --) L/= 2;
For (var I = numDigitsPastDecimal; I> 0 & L % 5 = 0; I --) L/= 5;

Return L;
}

Function GetFraction ()
{
Var dec = parseFloat (document. all ["txtDec"]. value );
Var digit = parseInt (document. all ["txt"]. value );
Document. all ["txtFraction"]. value = DecimalToFraction (dec, digit );
}
</Script>
 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.