简体   繁体   English

检查这两个数字是否等于n位有效数字

[英]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.

相关问题 计算小于或等于N的两个数字的对数,以使对数的数字总和为质数 - Count number of pairs of two numbers less than or equal to N such that Sum of the digits of numbers of pair is Prime 找出前两位数字和后两位数字之和的平方等于数字本身的所有四位数字 - find all four digit numbers for which the square of the sum of the first two digits and the last two digits equal the number itself 你如何将 java double 减少到 n 个有效数字? - How do you reduce a java double to n significant digits? 打印小于或等于56且数字总和大于10的两位数 JAVA - print two digit numbers which are less than or equal to 56 and the sum of digits is greater than 10 JAVA 如何在Java中使用两位有效数字将String转换为Long? - How do I convert String to Long with two significant digits in Java? 正则表达式检查小数点是否在两个数字之间,而不管小数点后的位数 - Regular Expression to check if decimal is between two numbers irrespective of number of digits after decimal point 如何检查一个方程是否有多位数的数字 - How to check if an equation has numbers with multiple digits Java中的大浮点数和双精度数会错误地打印/保留。 这是由于有效数字位数引起的吗? - Large float and double numbers in java printing/persisting incorrectly. Is this behavior due to number of significant digits? 在两个数字中寻找共享数字的麻烦 - Troubles with looking for shared digits in two numbers 检查两个不同的对象是否相等 - Check two different objects are equal or not
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM