简体   繁体   English

我的输出只需要小数点后两位

[英]Only need two decimal places in my output

How to get the double value that is only two digit after decimal point. 如何获取小数点后仅两位数字的double值。 The output I receive gives me BMI = 23.053626543209877 if my height input is 72 and weight 170. I'm not sure how to get rid of the trailing numbers after .05 如果我的身高输入为72体重为170,我收到的输出将给我BMI = 23.053626543209877。我不确定如何清除.05之后的尾随数字

Here is the code I have: 这是我的代码:

import java.util.Scanner;

public class bmi { 公共课bmi {

private static Scanner scanner;


public static void main(String[] args) {

    // Variables
    double height; 
    double weight; 
    double bmi; 

    Scanner keyboard = new Scanner(System.in);

    System.out.println("Please enter your height: ");
    height = keyboard.nextDouble();

    if(height<=0)
    {
        System.out.println("That's an invalid height.");
        return;
    }

    System.out.println("Please enter your weight: ");
    weight = keyboard.nextDouble();

    if(weight<=0)
    {
        System.out.println("That's an invalid weight.");
        return;
    }

    bmi =  calculateBMI(height, weight);


    System.out.println("BMI = " + bmi);

    System.out.println(bmiDescription(bmi));

    keyboard.close();
}


static double calculateBMI (double height, double weight) {
    return weight * 703 / (height * height);
}


static String bmiDescription (double bmi) {
    if (bmi < 18.5) {
        return "You are underweight.";
    } else {
        if (bmi > 25) {
            return "You are overweight.";
        } else {
            return "You are optimal.";
        }
    }
}

} }

You could use formatted io with printf , like 您可以将带格式的io与printf ,例如

double bmi = 23.053626543209877;
System.out.printf("BMI = %.2f%n", bmi);

Outputs (as I believe you wanted, and will even perform rounding) 输出(正如我所希望的那样,甚至可以进行舍入)

BMI = 23.05

It's always best to maintain precision when dealing with numbers but there are those certain situations where in depth precision is not really a main concern. 在处理数字时,最好始终保持精度,但是在某些情况下,深度精度并不是真正要考虑的问题。 If you want to set a specific double data type variable to a specific decimal precision then you could pass your Double type value to a method like this: 如果要将特定的double数据类型变量设置为特定的十进制精度,则可以将Double类型的值传递给这样的方法:

double bmi = round(23.053626543209877, 2);
System.out.println("BMI = " + bmi);

private static double round(double value, int precision) {
    int scale = (int) Math.pow(10, precision);
    return (double) Math.round(value * scale) / scale;
}

Output will be: 输出将是:

BMI = 23.05

As the method name suggests, the returned result is also rounded. 就像方法名称所暗示的那样,返回的结果也会四舍五入。

Here's a handy method I use: 这是我使用的一种便捷方法:

public static double round (double value, int precision) {
    int scale = (int) Math.pow(10, precision);
    return (double) Math.round(value * scale) / scale;
}

Just pass in your value, then pass in precision (the number of decimals to display). 只需传递您的值,然后传递精度(要显示的小数位数)。

For example: round(2.3891237, 2) will return 2.39 例如: round(2.3891237, 2)将返回2.39

In calculateBMI() do the following. 在calculateBMI()中执行以下操作。

NumberFormat numberFormat = NumberFormat.getIntegerInstance();
numberFormat.setMaximumFractionDigits(2);
numberFormat.format(x);

Where x will be your calculated double value. 其中x是您计算出的double值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM