java - 数学轮java

标签 java math rounding

我让项目将厘米转换为英寸。 我做到了: 我如何使用 Math.round 对数字进行四舍五入?

import java.util.Scanner;  

public class Centimer_Inch
{

public static void main (String[] args)
{
        // 2.54cm is 1 inch
       Scanner cm = new Scanner(System.in); //Get INPUT from pc-Keyboard
       System.out.println("Enter the CM:"); // Write input
       //double
       double centimeters = cm.nextDouble();
       double inches = centimeters/2.54;
       System.out.println(inches + " Inch Is " + centimeters + " centimeters");


    }
}

最佳答案

你可以这样做:

Double.valueOf(new DecimalFormat("#.##").format(
                                           centimeters)));  // 2 decimal-places

如果你真的想要Math.round :

(double)Math.round(centimeters * 100) / 100  // 2 decimal-places

使用 1000 可以保留 3 位小数,使用 10000 可以保留 4 位小数,等等。我个人更喜欢第一个选项。

关于java - 数学轮java,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13210491/

相关文章:

c++ - 在 C++ 中计算具有不同列数的数字的不同组合?

matlab arctanh 函数

iphone - ios 中的金额值截断

python - Python 中的 float 舍入

java - 解析从 Arduino 上的多个传感器读取的数据

java - 如何从 Firestore 中具有特殊字符的文档中获取数据

java - java 中是否有用于批量处理 Set/List 的实用程序 Api/方法?

java - 一个区域内的排列数量?

sql - T-SQL 四舍五入到 0.0、0.5 或 1.0

java - 我如何安全地处理 JVM 上的 key Material 缓冲区并将其归零?