It is a method to calculate the number of years and the number of remaining months from the number of months. (Written in Java)
35 months is 2 years 11 months
It feels like this.
The code looks like this.
YearMonth.java
public class YearMonth{
public static void main(String args[]){
int monthCount = 35;
System.out.println(monthCount + "Months" + monthCount/12 + "Year" + monthCount%12 + "Month");
}
}
The 35 months stored in the variable monthCount have been converted to years.
Years and months were calculated separately.
For the year, divide 35 months by 12 months (1 year) and round off the decimal point with Math.floor. And if it is left as it is, the decimal point will be .0, so put (int) in front of it to make it an integer.
For months, it was possible to calculate by dividing 35 months to 12 months (1 year).
Recommended Posts