Enjoy an ad free experience by logging in. Not a member yet? Register.

Results 1 to 8 of 8

09292011, 06:44 AM #1
 Join Date
 Sep 2011
 Posts
 1
 Thanks
 0
 Thanked 0 Times in 0 Posts
Help with convert improper fraction into mixed number
I am new with java and now i have an assignment to convert an improper fraction like 9/5 into a mixed number 1 4/5. I got how to convert it but if i have 4/5 and the output should give back 4/5 not 0 4/5.
Is there any way I could solve this problem? (I haven't learn "if then" yet? Just start from beginning )
Thanks
09292011, 06:45 AM
#2
 Join Date
 Sep 2011
 Location
 Sweden
 Posts
 154
 Thanks
 1
 Thanked 22 Times in 22 Posts
This is the JavaScript forum
09292011, 06:54 AM
#3
 Join Date
 Sep 2011
 Location
 Sweden
 Posts
 154
 Thanks
 1
 Thanked 22 Times in 22 Posts
In JavaScript:
Code:String.prototype.intAndFractionFromFraction = function(){ var x = this; x = x.split('/'); var a = x[0]; var b = x[1]; if(a/1 != a  b/1 != b){return 0}; var floatie = a/b; var whole = Math.floor(floatie); var rest = (floatie  whole) * b; return whole + ' ' + rest + '/' + b }; alert('9/5'.intAndFractionFromFraction());
09292011, 05:17 PM
#4
 Join Date
 Mar 2006
 Posts
 728
 Thanks
 35
 Thanked 132 Times in 123 Posts
/* sample valuesCode:function properFraction(string){ if(typeof string!= 'string'){ string= prompt('Enter a number to convert to a proper fraction', '1 and 26/6'); } /* first break up the input into an(optional) whole number with a space or other separator, and then the numerator and denominator of the fraction. You can parse string input in several ways, this uses a regular expression. */ var whole, least, increment, numerator, denominator, n=/^\s*(\d+([^\d\/]+))?(\d+)\/(\d+)\s*$/.exec(string); if(!n) return string; whole= n[1]? parseInt(n[1]): 0; separator= n[2]  ' '; numerator= +n[3]; denominator= +n[4]; /* if the numerator is larger than the denominator, add the rounded division result to the whole number part of the input, or to 0 if there was no whole number part. The modulus(%) operator gives the remainder of the division operation, which will be the new numerator. */ if(numerator>= denominator){ whole+= Math.floor(numerator/denominator); numerator= numerator%denominator; if(!numerator) return whole; } /* A proper fraction is usually considered to be the simplest expression of the number 12/24 should be expressed as 1/2, and 15/45 as 1/3. You can use the modulus operator again to simplify it. */ least= numerator; while(least> 1){ if(numerator%least== 0 && denominator%least== 0){ numerator/= least; denominator/= least; } if(numerator<least) least= numerator; else least } /* The return value is assembled from the new integral part and separator, if any, and the simplified fractional parts. */ whole= whole? whole+separator: ''; return whole+ numerator+'/'+denominator; }
properFraction('9/5')= 1 4/5
properFraction('4/5')= 4/5
properFraction('1 26/8')= 4 1/4
properFraction('3 and 34/6')= 8 and 2/3
properFraction('3 and 42/15')= 5 and 4/5
properFraction('1/2')= 1/2
properFraction('72/4')= 18
properFraction('1 and 118/16')= 8 and 3/8
properFraction('9 42/8')= 14 1/4
*/
Last edited by mrhoo; 09292011 at 06:49 PM. Reason: caught number input
09292011, 05:48 PM
#5
 Join Date
 Jun 2002
 Location
 London, England
 Posts
 18,370
 Thanks
 204
 Thanked 2,573 Times in 2,551 Posts
mrhoo  something wrong here!
alert (properFraction(9/5)) gives me 1.8
alert (properFraction(26/8)) gives me 3.25
alert (properFraction(3 and 34/6)) // does not work at all
ironboy 
alert('24/5'.intAndFractionFromFraction()); // gives 4 3.999999999999999/5
Last edited by Philip M; 09292011 at 05:51 PM.
All the code given in this post has been tested and is intended to address the question asked.
Unless stated otherwise it is not just a demonstration.
Users who have thanked Philip M for this post:
mrhoo (09292011)
09292011, 06:35 PM
#6
 Join Date
 Mar 2006
 Posts
 728
 Thanks
 35
 Thanked 132 Times in 123 Posts
Phillip The code only works on string input.
I edited it to catch number input with a prompt, but in 'real life' it would come from an input field...
Last edited by mrhoo; 09292011 at 06:52 PM.
Users who have thanked mrhoo for this post:
Philip M (09292011)
09292011, 06:43 PM
#7
 Join Date
 Jun 2002
 Location
 London, England
 Posts
 18,370
 Thanks
 204
 Thanked 2,573 Times in 2,551 Posts
Here is my attempt:
A variant:Code:<script type = "text/javascript"> function reduce(num,den) { var gcd = function gcd(a,b){ return b ? gcd(b, a%b) : a; } gcd = gcd(num,den); return [num/gcd + "/" + den/gcd]; } function improper(num,den) { var int = parseInt(num/den); var rem = num%den; var y = reduce (rem,den) if (y == "0/1") {y = ""}; if (int == 0) {int = ""} return (int + " " + y); } alert (improper(9,5)); alert (improper (34,6)); alert (improper (72,4)); alert (improper (26,8)); alert (improper (80,11)) alert (improper (80,10)) </script>
Code:<script type = "text/javascript"> function reduce(num,den) { var gcd = function gcd(a,b){ return b ? gcd(b, a%b) : a; } gcd = gcd(num,den); return [num/gcd + "/" + den/gcd]; } function improper(num) { var x = num.split("/"); var int = parseInt(x[0]/x[1]); var rem = x[0]%x[1]; var y = reduce (rem,x[1]); if (y == "0/1") {y = ""}; if (int == 0) {int = ""} return (int + " " + y); } alert (improper ("9/5")); // STRING INPUT alert (improper ("34/6")); alert (improper ("72/4")); alert (improper ("26/8")); alert (improper ("80/11")); alert (improper ("80/10")); </script>
Last edited by Philip M; 09292011 at 07:46 PM.
All the code given in this post has been tested and is intended to address the question asked.
Unless stated otherwise it is not just a demonstration.
09292011, 06:53 PM
#8