Home>

Please write your question in detail here.
I'm doing javascript,
I would like to calculate that put a number in int in java
I'd like to make the decimal point the result of putting that number. please tell me. I'm a beginner so please

int num1 = 0;
int num2 = 0;
double resultNum;

try {
num1 = Integer.parseInt (request.getParameter ("num1"));
num2 = Integer.parseInt (request.getParameter ("num2"));
resultNum = num2/(num1 * num1);← This is not possible. .
} catch (NumberFormatException e) {
resultNum = 0;
}
request.setAttribute ("num1", num1);
request.setAttribute ("num2", num2);
request.setAttribute ("resultNum", resultNum);

  • Answer # 1

    Is it wrong to receive double from the beginning?

    double num1 = 0;
    double num2 = 0;
    double resultNum;
    try {
        num1 = Double.parseDouble (request.getParameter ("num1"));
        num2 = Double.parseDouble (request.getParameter ("num2"));
        resultNum = num2/(num1 * num1);
    } catch (NumberFormatException e) {
        resultNum = 0;
    }
    request.setAttribute ("num1", (int) num1);
    request.setAttribute ("num2", (int) num2);
    request.setAttribute ("resultNum", resultNum);

    If you cast, theshortestis probably this.

    resultNum = 1. * num2/(num1 * num1);

    It's hard to say that it's easy for everyone to read, so we don't recommend it.

  • Answer # 2

      

    resultNum = num2/(num1 * num1);← This is not possible. .

    Maybe the result has no decimal places?

      

    resultNum = (double) num2/(double) (num1 * num1);

    Please cast

    . (Is either one okay?)
    Calculations between ints are done with ints (no decimal places) and then converted to double.

  • Answer # 3

    It is said that int is double, but primitive floating point numbers such as double may have inaccurate decimal representation of decimal numbers. Is that okay?

    If there is no problem, you can use the method of casting to double for int type variables in the calculation formula as written by pepperleaf. Refer to pepperleaf for a specific example.

    If it is inconvenient, you should consider using java.math.BigDecimal instead of double.
    Although it is necessary to specify the required number of digits after the decimal point and the rounding method (rounding or truncation), it is obtained as follows.

    The following is an example of rounding to two decimal places.

    BigDecimal resultNum = BigDecimal.valueOf (num2) .divide (
        BigDecimal.valueOf (num1) .pow (2), 2, BigDecimal.ROUND_HALF_UP);
    System.out.println (resultNum.toPlainString ());

    int num1 = 3;int num2 = 7;execution result

    0.78


    The calculation result is a rounded decimal of 0.7777 ..., so the third decimal place is rounded off.
    Note that if you do not specify the number of digits after the decimal point, an infinite number of digits is required and BigDecimal's divide method throws an ArithmeticException.

    Error in case of BigDecimal.valueOf (num2) .divide (BigDecimal.valueOf (num1) .pow (2))

    Exception in thread "main" java.lang.ArithmeticException: Non-terminating decimal expansion;no exact representable decimal result.
        at java.base/java.math.BigDecimal.divide (BigDecimal.java:1722)
        at Main.main (Main.java:22)

    For the code in question, you can setAttribute the result of toPlainString as specified in the argument of System.out.println above.

    request.setAttribute ("resultNum", resultNum.toPlainString ());

    JavaSE 8 BigDecimal javadoc
    I think how to use BigDecimal will be helpful.