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
-
Answer # 2
resultNum = num2/(num1 * num1);← This is not possible. .
Maybe the result has no decimal places?
resultNum = (double) num2/(double) (num1 * num1);
. (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 result0.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.
Related articles
- i don't know how to change the java formula
- i want to convert an int type array to a str type array in java
- java: how to call $0 in a function in replaceall
- java i want to change the format of the value called from a different file
- java - how to use try-catch
- java - i want to change the background color of a row in a thymeleaf table under certain conditions
- i want to know how to raise the java version to 520
- java - change every time you press the color of the android studio button
- java - when you do not want to change the value of the property (field)
- java - i want to know how to use if (!"variable"hasnextline()) break;
- python - how to inherit dictionary type
- java ee - i want to know how to use servletcontextlistener
- java - i don't know how to add up the numbers in the list
- java - [jsp] please tell me how to use iterator
- change java output text color
- how to use java try-catch statement
- java - how to get data at jsp screen transition
- python - how to change the font size of the top menu bar of tkinter
- java - i want to change the text of the answer button depending on the problem
- how to sort java11 list
Is it wrong to receive double from the beginning?
If you cast, theshortestis probably this.
It's hard to say that it's easy for everyone to read, so we don't recommend it.