java - How do i get a 2 decimal answer in this code? because i get more than 2 -
this question has answer here:
- how round number n decimal places in java 28 answers
import java.util.scanner; public class bmi { public static void main(string[] args) { scanner scan = new scanner(system.in); float height,heightsquared,bmi; int weight; system.out.println("your height in meters:"); height = scan.nextfloat(); system.out.println("your weight in kilograms:"); weight = scan.nextint(); heightsquared = (height*height); bmi = weight/heightsquared; if (bmi < 18.5) { system.out.println("your bmi result:"+ bmi + "(under-weight)"); } else if (bmi > 18.4 && bmi < 24.6) { system.out.println("your bmi result:"+ bmi + "(normal-weight)"); } else if (bmi > 24.9 && bmi < 30) { system.out.println("your bmi result:"+ bmi + "(over-weight)"); } else { system.out.println("your bmi result:"+ bmi + "(obesse)"); } } }
this code getting body mass index (bmi) of person it's okay when running problem how turn answer in 2 decimals? example:
your height in meters: 1.70 weight in kilograms: 50 bmi result:17.301032(under-weight)
i want output 17.30 or in 2 decimals. should change?
try this. can use %2f
2 decimal places
your bmi result:"+ string.format("%.2f", bmi)
Comments
Post a Comment