Hello and welcome to our community! Is this your first visit?
Enjoy an ad free experience by logging in. Not a member yet? Register.

# Thread: Help with convert improper fraction into mixed number

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

• This is the JavaScript forum

• 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
};

• Code:
```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
*/

• mrhoo - something wrong here!

alert (properFraction(3 and 34/6)) // does not work at all

ironboy -

• ## Users who have thanked Philip M for this post:

mrhoo (09-29-2011)

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

• ## Users who have thanked mrhoo for this post:

Philip M (09-29-2011)

• Here is my attempt:-

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

</script>```
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) {
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

</script>```

• Originally Posted by mrhoo
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! Mea culpa!

•

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•