[英]Check if the two numbers are equal to n significant digits
I figured out the main portion of the code but I can't figure out how to get it so it will only read the difference up to the however many decimal points the user inputs. 我想出了代码的主要部分,但我无法弄清楚如何获取它所以它只会读取差异,直到用户输入的多少小数点。 So how would I go about being able to take the user input( it would be third number typed in) and find the whether the first two numbers are equal to that decimal point.
那么我将如何获取用户输入(将输入第三个数字)并找出前两个数字是否等于该小数点。 The goal of the program is to check if the two numbers are equal to n(a number the user inputs) significant digits.
该程序的目标是检查这两个数字是否等于n(用户输入的数字)有效数字。
Sample Input: 5.124 5.125 2 样本输入:5.124 5.125 2
Sample output: Difference = .001 , Numbers are equal to 2 decimal places 样本输出:差值= .001,数字等于2位小数
import java.util.Scanner;
public class Equal {
public static void main(String[] args) {
Scanner input = new Scanner( System.in );
String diff="diffrence =";
double num1;
double num2;
double num3;
double calc;
while(input.hasNext()){
num1 = Double.parseDouble(input.next());
num2 = Double.parseDouble(input.next());
num3 = Double.parseDouble(input.next());
calc = Math.abs(num1-num2);
System.out.printf("%s %.10f", diff, calc);
}
}
}
Here is an example and a junit test case for ordinary doubles. 这是一个普通双打的例子和junit测试用例。 Hope this helps.
希望这可以帮助。
public boolean doublesSameNumberOfDecimalPlaces(double x,double y,int numberOfDecimals){
x = (long)(x * Math.pow(10, numberOfDecimals));
y = (long)(y * Math.pow(10, numberOfDecimals));
return x==y;
}
@Test
public void TestDoublesSameNumberOfDecimalPlaces(){
double x = 3.141593;
double y = 3.14159265359;
assertTrue(doublesSameNumberOfDecimalPlaces(x,y,0));
assertTrue(doublesSameNumberOfDecimalPlaces(x,y,1));
assertTrue(doublesSameNumberOfDecimalPlaces(x,y,2));
assertTrue(doublesSameNumberOfDecimalPlaces(x,y,3));
assertTrue(doublesSameNumberOfDecimalPlaces(x,y,4));
assertTrue(doublesSameNumberOfDecimalPlaces(x,y,5));
assertFalse(doublesSameNumberOfDecimalPlaces(x,y,6));
}
A nice way using BigDecimal
: 使用
BigDecimal
的好方法:
import java.math.BigDecimal;
import java.math.MathContext;
import java.math.RoundingMode;
import java.util.Scanner;
public class Equal {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
String num1;
String num2;
int num3;
while (input.hasNext()) {
// First number
num1 = input.next();
// Second number
num2 = input.next();
// Number of significant figures
num3 = Integer.parseInt(input.next());
MathContext mc = new MathContext(num3, RoundingMode.HALF_UP);
BigDecimal bdNum1 = new BigDecimal(num1, mc);
BigDecimal bdNum2 = new BigDecimal(num2, mc);
System.out.println(bdNum1.equals(bdNum2));
}
input.close();
}
}
Depending on your concrete needs you might want to play around with the RoundingMode
. 根据您的具体需求,您可能想要使用
RoundingMode
。
Sample input: 样本输入:
1.2345678
1.2345679
7
Sample output: 样本输出:
true
Sample input: 样本输入:
0.123
0.124
3
Sample output: 样本输出:
false
Note that the behaviour might not be as expected in special cases like: 请注意,在特殊情况下,行为可能与预期不符,如:
Input: 输入:
0.10000
0.1000
8
Output: 输出:
false
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.