In most cases, the actual input is not a floating point number, but you have a width , a height , and you get a floating point number dividing them.
In this case, the problem can be reduced to detecting the greatest common factor :
private static int greatestCommonDivisor(int a, int b) {
return (b == 0) ? a : greatestCommonDivisor(b, a % b);
}
private static String aspectRatio(int width, int height) {
int gcd = greatestCommonDivisor(width, height);
if (width > height) {
return String.Format("{0} / {1}", width / gcd, height / gcd);
} else {
return String.Format("{0} / {1}", height / gcd, width / gcd);
}
}
source
share