...

View Full Version : Help with convert improper fraction into mixed number



ethannguyen93
09-29-2011, 06:44 AM
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

ironboy
09-29-2011, 06:45 AM
This is the JavaScript forum :cool:

ironboy
09-29-2011, 06:54 AM
In JavaScript:

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());

mrhoo
09-29-2011, 05:17 PM
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;
}

/* sample values

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
*/

Philip M
09-29-2011, 05:48 PM
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

mrhoo
09-29-2011, 06:35 PM
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...

Philip M
09-29-2011, 06:43 PM
Here is my attempt:-


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

A variant:-


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

Philip M
09-29-2011, 06:53 PM
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...

Aaarggghhh! :o Mea culpa!



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum