Question is from here:
This question appears regularly at beginning-programmer discussion sites:
Write a function that takes a floating point number f and an integer n and rounds the floating point number to n places after the decimal point. For instance, given the floating point number 1000/7, rounding to 3 places would produce 142.857, rounding to 2 places would produce 142.86, rounding to 1 place would produce 142.9, and rounding to 0 places would produce 143. For extra credit, allow n to be negative, indicating rounding before the decimal point; for instance, rounding 1000/7 to -1 places would produce 140.
It’s not fair to use a built-in round function if your language provides one.
Your task is to write the function described above.
My Solution:
Given input “1000/7,3″:
- calculated the fraction: 142.85714285714286
- multiplied this number to 10^3, 142857.14285714286
- result was converted to Long. 142857
- checked if the last digit is greater than or eqaul to 5
- if yes add 1 to the number, otherwise subtract last digit from the number, 142858
- Divided the number by 10^3 and convert it back to double, 142.858
import java.io.*;
public class Floating_Point_Rounding {
public static void main(String[] args) {
BufferedReader r = new java.io.BufferedReader (new InputStreamReader (System.in));
String s;
try {
System.out.println("Enter a number");
s = r.readLine();
round(s);
while(true){
System.out.println("Enter a number");
s = r.readLine();
round(s);
}
} catch (IOException e) {
e.printStackTrace();
}
}
private static void round(String s){
//input is of format xxx/x,x
String[] parts = s.split(",");
String[] fraction = parts[0].split("/");
double d1 = Double.parseDouble(fraction[0]);//numerator
double d2 = Double.parseDouble(fraction[1]);//denominator
double ans = d1/d2;//fraction
double n = Double.parseDouble(parts[1]);//decimal point
long ans2, ans3; String s1; double finalAns;
if(n > 0){
ans2 = (long) Math.pow(10, n + 1);
ans3 = (long) (ans2 * ans);
s1 = Long.toString(ans3);
if(Character.getNumericValue(s1.charAt(s1.length() - 1)) >= 5 ){
ans3 += (10 - Character.getNumericValue(s1.charAt(s1.length() - 1)));
} else{
ans3 -= 1;
}
finalAns = (double) ans3 / ans2;
} else if (n == 0){
ans2 = 10;
ans3 = (long) (ans2 * ans);
s1 = Long.toString(ans3);
if(Character.getNumericValue(s1.charAt(s1.length() - 1)) >= 5 ){
ans3 += 10;
}
finalAns = ans3 /=10;
} else {
ans2 = (long) ans;
int r = (int) Math.abs(n);
s1 = Long.toString(ans2);
if(Character.getNumericValue(s1.charAt(s1.length() - r)) < 5 ){
String s2 = s1.substring(s1.length() - r, s1.length());
ans2 -= Integer.parseInt(s2);
} else {
String s2 = s1.substring(s1.length() - r, s1.length());
ans2 += (Math.pow(10, r) - Integer.parseInt(s2));
}
finalAns = ans2;
}
System.out.println("Answer: " + finalAns);
}
}
Output:
Enter numbers: 1000/7,3 Answer: 142.857 Enter numbers: 1000/7,2 Answer: 142.86 Enter numbers: 1000/7,1 Answer: 142.9 Enter numbers: 1000/7,0 Answer: 143.0 Enter numbers: 1000/7,-1 Answer: 140.0 Enter numbers: 1000/7,-2 Answer: 100.0
Reblogged this on CompSci log.