[Personal memo] Java data type is annoying

I didn't understand Math.round ()

The story when I was advancing Progate

Height (height) and weight (weight) are defined in double type, and use it to calculate BMI (bmi). Since BMI is an integer type, it is necessary to round off the calculated value. You could use the Math.round () method.

What happened at first

public static int bmi(double weight,double height){
 int bmi=Math.round(weight/height/height);
 return bmi;
}

I wrote it like this. I thought that if I rounded it with the Math.round () method, it would naturally become an int type. But it returns an error.

If you look it up

Apparently, the value rounded off by the Math.round () method seems to be a long type, and if you want to use it as an int type, you have to convert it. So

public static int bmi(double weight,double height){
 int bmi=int(Math.round(weight/height/height));
 return bmi;
}

I rewrote it. If you think that the value rounded by Math.round () is now an int type, you will get an error again.

I misunderstood it as Python, and the type conversion method was wrong.

Finally

public static int bmi(double weight,double height){
 int bmi=(int)Math.round(weight/height/height);
 return bmi;
}

By doing so, it finally processed without error. I'm happy.

It was a memorandum memo.

This lesson

-~~ If you round off with the Math.round () method, it will be a long type ~~ --Math.round () method has Math.round (float) and Math.round (double), (float) will be int type, and (double) will be long type. --When converting data type in Java, you have to say "(data type) variable name;"

It was that.

Referenced page

When I was looking for a way to deal with various things, I found a page of teratail that was asking the same question in the same situation, and I got nothing. URL:https://teratail.com/questions/114663

Recommended Posts

[Personal memo] Java data type is annoying
Java learning memo (data type)
[Personal memo] Java development environment is ready
[Java] Data type ①-Basic type
Organized memo in the head (Java --Data type)
Java HashMap, entrySet [Personal memo]
Personal memo: Metaprogramming with Java reflection
[Java] Data type / matrix product (AOJ ⑧ Matrix product)
Java memo
Java date data type conversion (Date, Calendar, String)
What is the right migration data type? ??
Use PostgreSQL data type (jsonb) from Java
[Java ~ Variable definition, type conversion ~] Study memo
[Java] Data type / string class cheat sheet
Display message dialog in java (personal memo)
Java type conversion
java anything memo
What is java
Java Silver memo
[JAVA] Stream type
java, maven memo
The intersection type introduced in Java 10 is amazing (?)
Java Optional type
[Processing x Java] Data type and object-oriented programming
Java SE 7 memo
What is Java <>?
Memo: [Java] Get Raspberry Pi data by SFTP
Java double type
What is Java
java anything memo 2
Java specification memo
Java pattern memo
Java variable declaration, initialization, data type (cast and promotion)
Data acquisition method memo when there is HashMap in HashMap
Java getClass () is incompatible with generics (or type variables)
[Personal memo] Make a simple deep copy in Java
About Java basic data types and reference type memory
Learning memo when learning Java for the first time (personal learning memo)
Java development environment memo
java basic knowledge memo
Java Kuche Day memo
[Java ~ Method ~] Study memo (5)
java se 8 programmer Ⅰ memo
Java paid private memo
What is Java Encapsulation?
Java static [Personal summary]
[Java, Kotlin] Type Variance
Java class type field
Type determination in Java
Java study # 1 (typical type)
[Java ~ Array ~] Study memo 4
Java learning memo (basic)
java lambda expression memo
(Memo) Java for statement
What is Java technology?
Personal summary about Java
What is Java API-java
Java lambda expression [memo]
[Java] Main data types
[Java] About enum type
Java learning memo (interface)