import java.math.BigDecimal;
import java.text.DecimalFormat;
public class TestGetInt {
public static void main(String[] args) {
double i = 2, j = 2.1, k = 2.5, m = 2.9;
System.out.println("舍掉小数取整:Math.floor(" + i + ")=" + (int) Math.floor(i));
System.out.println("舍掉小数取整:Math.floor(" + j + ")=" + (int) Math.floor(j));
System.out.println("舍掉小数取整:Math.floor(" + k + ")=" + (int) Math.floor(k));
System.out.println("舍掉小数取整:Math.floor(" + m + ")=" + (int) Math.floor(m));
System.out.println();
System.out.println("舍掉小数取整:Math.floor(-" + i + ")=" + (int) Math.floor(-i));
System.out.println("舍掉小数取整:Math.floor(-" + j + ")=" + (int) Math.floor(-j));
System.out.println("舍掉小数取整:Math.floor(-" + k + ")=" + (int) Math.floor(-k));
System.out.println("舍掉小数取整:Math.floor(-" + m + ")=" + (int) Math.floor(-m));
System.out.println();
// 这段被注释的代码不能正确的实现四舍五入取整
System.out.println("四舍五入取整:Math.rint(" + i + ")=" + (int) Math.rint(i));
System.out.println("四舍五入取整:Math.rint(" + j + ")=" + (int) Math.rint(j));
System.out.println("四舍五入取整:Math.rint(" + k + ")=" + (int) Math.rint(k));
System.out.println("四舍五入取整:Math.rint(" + m + ")=" + (int) Math.rint(m));
System.out.println();
System.out.println("四舍五入取整:Math.rint(-" + i + ")=" + (int) Math.rint(-i));
System.out.println("四舍五入取整:Math.rint(-" + j + ")=" + (int) Math.rint(-j));
System.out.println("四舍五入取整:Math.rint(-" + k + ")=" + (int) Math.rint(-k));
System.out.println("四舍五入取整:Math.rint(-" + m + ")=" + (int) Math.rint(-m));
System.out.println();
System.out.println("DecimalFormat四舍五入取整:(" + i + ")="
+ new DecimalFormat("0").format(i));
System.out.println("DecimalFormat四舍五入取整:(" + j + ")="
+ new DecimalFormat("0").format(j));
System.out.println("DecimalFormat四舍五入取整:(" + k + ")="
+ new DecimalFormat("0").format(k));
System.out.println("DecimalFormat四舍五入取整:(" + m + ")="
+ new DecimalFormat("0").format(m));
System.out.println();
System.out.println("DecimalFormat四舍五入取整:(-" + i + ")="
+ new DecimalFormat("0").format(-i));
System.out.println("DecimalFormat四舍五入取整:(-" + j + ")="
+ new DecimalFormat("0").format(-j));
System.out.println("DecimalFormat四舍五入取整:(-" + k + ")="
+ new DecimalFormat("0").format(-k));
System.out.println("DecimalFormat四舍五入取整:(-" + m + ")="
+ new DecimalFormat("0").format(-m));
System.out.println();
System.out.println("BigDecimal四舍五入取整:(" + i + ")="
+ new BigDecimal("2").setScale(0, BigDecimal.ROUND_HALF_UP));
System.out.println("BigDecimal四舍五入取整:(" + j + ")="
+ new BigDecimal("2.1").setScale(0, BigDecimal.ROUND_HALF_UP));
System.out.println("BigDecimal四舍五入取整:(" + k + ")="
+ new BigDecimal("2.5").setScale(0, BigDecimal.ROUND_HALF_UP));
System.out.println("BigDecimal四舍五入取整:(" + m + ")="
+ new BigDecimal("2.9").setScale(0, BigDecimal.ROUND_HALF_UP));
System.out.println();
System.out.println("BigDecimal四舍五入取整:(-" + i + ")="
+ new BigDecimal("-2").setScale(0, BigDecimal.ROUND_HALF_UP));
System.out.println("BigDecimal四舍五入取整:(-" + j + ")="
+ new BigDecimal("-2.1").setScale(0, BigDecimal.ROUND_HALF_UP));
System.out.println("BigDecimal四舍五入取整:(-" + k + ")="
+ new BigDecimal("-2.5").setScale(0, BigDecimal.ROUND_HALF_UP));
System.out.println("BigDecimal四舍五入取整:(-" + m + ")="
+ new BigDecimal("-2.9").setScale(0, BigDecimal.ROUND_HALF_UP));
System.out.println();
System.out.println("凑整:Math.ceil(" + i + ")=" + (int) Math.ceil(i));
System.out.println("凑整:Math.ceil(" + j + ")=" + (int) Math.ceil(j));
System.out.println("凑整:Math.ceil(" + k + ")=" + (int) Math.ceil(k));
System.out.println("凑整:Math.ceil(" + m + ")=" + (int) Math.ceil(m));
System.out.println();
System.out.println("凑整:Math.ceil(-" + i + ")=" + (int) Math.ceil(-i));
System.out.println("凑整:Math.ceil(-" + j + ")=" + (int) Math.ceil(-j));
System.out.println("凑整:Math.ceil(-" + k + ")=" + (int) Math.ceil(-k));
System.out.println("凑整:Math.ceil(-" + m + ")=" + (int) Math.ceil(-m));
System.out.println();
System.out.println("四舍五入取整:Math.round(" + i + ")=" + (int) Math.round(i));
System.out.println("四舍五入取整:Math.round(" + j + ")=" + (int) Math.round(j));
System.out.println("四舍五入取整:Math.round(" + k + ")=" + (int) Math.round(k));
System.out.println("四舍五入取整:Math.round(" + m + ")=" + (int) Math.round(m));
System.out.println();
System.out.println("四舍五入取整:Math.round(-" + i + ")=" + (int) Math.round(-i));
System.out.println("四舍五入取整:Math.round(-" + j + ")=" + (int) Math.round(-j));
System.out.println("四舍五入取整:Math.round(-" + k + ")=" + (int) Math.round(-k));
System.out.println("四舍五入取整:Math.round(-" + m + ")=" + (int) Math.round(-m));
}
}
舍掉小数取整:Math.floor(2.0)=2
舍掉小数取整:Math.floor(2.1)=2
舍掉小数取整:Math.floor(2.5)=2
舍掉小数取整:Math.floor(2.9)=2
舍掉小数取整:Math.floor(-2.0)=-2
舍掉小数取整:Math.floor(-2.1)=-3
舍掉小数取整:Math.floor(-2.5)=-3
舍掉小数取整:Math.floor(-2.9)=-3
四舍五入取整:Math.rint(2.0)=2
四舍五入取整:Math.rint(2.1)=2
四舍五入取整:Math.rint(2.5)=2
四舍五入取整:Math.rint(2.9)=3
四舍五入取整:Math.rint(-2.0)=-2
四舍五入取整:Math.rint(-2.1)=-2
四舍五入取整:Math.rint(-2.5)=-2
四舍五入取整:Math.rint(-2.9)=-3
DecimalFormat四舍五入取整:(2.0)=2
DecimalFormat四舍五入取整:(2.1)=2
DecimalFormat四舍五入取整:(2.5)=2
DecimalFormat四舍五入取整:(2.9)=3
DecimalFormat四舍五入取整:(-2.0)=-2
DecimalFormat四舍五入取整:(-2.1)=-2
DecimalFormat四舍五入取整:(-2.5)=-2
DecimalFormat四舍五入取整:(-2.9)=-3
BigDecimal四舍五入取整:(2.0)=2
BigDecimal四舍五入取整:(2.1)=2
BigDecimal四舍五入取整:(2.5)=3
BigDecimal四舍五入取整:(2.9)=3
BigDecimal四舍五入取整:(-2.0)=-2
BigDecimal四舍五入取整:(-2.1)=-2
BigDecimal四舍五入取整:(-2.5)=-3
BigDecimal四舍五入取整:(-2.9)=-3
凑整:Math.ceil(2.0)=2
凑整:Math.ceil(2.1)=3
凑整:Math.ceil(2.5)=3
凑整:Math.ceil(2.9)=3
凑整:Math.ceil(-2.0)=-2
凑整:Math.ceil(-2.1)=-2
凑整:Math.ceil(-2.5)=-2
凑整:Math.ceil(-2.9)=-2
四舍五入取整:Math.round(2.0)=2
四舍五入取整:Math.round(2.1)=2
四舍五入取整:Math.round(2.5)=3
四舍五入取整:Math.round(2.9)=3
四舍五入取整:Math.round(-2.0)=-2
四舍五入取整:Math.round(-2.1)=-2
四舍五入取整:Math.round(-2.5)=-2
四舍五入取整:Math.round(-2.9)=-3
分享到:
相关推荐
在Java编程语言中,取整函数是用于将浮点数转换为整数的关键操作,它在各种场景下都非常常见,例如处理数学计算、数据分析或控制流。Java提供了多种方法来执行取整操作,以满足不同需求。让我们深入探讨这些方法。 ...
根据标题“JAVA取整计算器源码”和描述,这个计算器程序目前只能处理整数的输入和计算,并且结果也会被强制取整。这涉及到两个主要的知识点:整数运算和取整方法。 首先,我们来谈谈整数运算。在JAVA中,整数类型...
java技术,对象操作:数值取整和保留两位小数,将数值转换成字符串。
在Java编程语言中,"取整"是一个常见的数学操作,用于将浮点数转换为整数,去掉小数部分。这个过程可以分为多种类型,包括向下取整、向上取整、四舍五入等。本篇文章将深入探讨Java中如何进行这些取整操作。 1. 向...
浅析Java中的取整(/)和求余(%) Java语言中,取整和求余是两个基本的数学运算符,分别用“/”和“%”表示。下面我们将详细介绍这两个运算符的使用和注意事项。 一、取整运算符(/) 取整运算符是将一个数字...
例如,将十进制小数转换为二进制通常需要乘以2并取整,然后不断重复这个过程直到小数部分为0。 总之,理解并熟练掌握不同进制之间的转换是编程基础之一,这对于理解和处理计算机中的数据至关重要。无论是进行数值...
在IT行业中,百分比计算是常见的任务,尤其是在编程语言如Java中。本篇文章将深入探讨如何使用Java来实现百分比的计算,同时结合提供的`PercentTool.java`源码,我们将一起解析并理解其中的关键知识点。 首先,让...
最后,"JAVA取整计算器源码"是一个基础但重要的项目,它展示了Java在数值计算和用户界面设计方面的应用。Java Swing或JavaFX库可用于构建图形用户界面(GUI),包含按钮、文本框等组件。开发者可以学习到事件监听、...
输入一个整十进制数n,把它转化为二进制,小数部分还没有考虑。。。
HelloWorldApp.java 第一个用Java开发的应用程序。 firstApplet.java 第一个用Java开发的Applet小程序。 firstApplet.htm 用来装载Applet的网页文件 第2章 示例描述:本章介绍开发Java的基础语法知识。 ...
这时,Java提供了三种内置函数来实现不同的取整操作。本文将详细介绍这些函数:Math.floor()、Math.rint()以及Math.ceil(),并探讨它们各自的用法和特点。 1. **舍掉小数取整:Math.floor()** Math.floor()函数是...
Java中的相除(/)和取余(%)的实现方法 Java 中的相除和取余运算是两个基本的数学运算符,它们在 Java 中的实现方法是非常重要的。下面将对 Java 中的相除和取余运算进行详细的介绍。 相除运算 在 Java 中,...
在Java编程语言中,四舍五入是一种常见的数学操作,用于处理浮点数或小数,使其更加符合实际应用需求。本文将详细解析如何在Java中实现四舍五入,并探讨不同方法的适用场景。 ### Java四舍五入的基础概念 四舍五入...
然而,在 Java 语言中,rint 函数不能正确地实现四舍五入取整。例如: ```java double i = 2.5; System.out.println("四舍五入取整:Math.rint(2.5) = " + (int)Math.rint(i)); ``` 上述代码不能正确地实现四舍五入...
Java编程语言在实际应用中经常会遇到各种问题,尤其是在处理字符编码、数据取整以及用户交互等方面。以下是对这些常见问题的详细分析和解决方案。 一、Jsp 页面中 request 获取的中文参数是乱码 在Java Web开发中...
《深入剖析Java.lang.Number类》 Java.lang.Number是Java编程语言中的一个抽象基类,它为所有数值类型提供了基础框架。Number类的主要作用是提供一套通用的方法,将表示的数值转换为不同类型的值,如byte、double、...
- 乘基数取整法:适用于二进制转换为任意基数。 Java中还有一些内置的函数来辅助实现进制转换,比如`Integer.toString(int num, int radix)`可以将十进制数转换成任意进制的字符串表示,而`Integer.parseInt(String...
3. `(x++)/3`的结果是0,因为整数除法向下取整。 4. `x 和`x >= y`的逻辑值分别为`true`和`false`。 5. 抽象方法和最终方法是Java中控制代码重写的方式。 6. 创建包的语句`package MyPackage;`应放在源文件开头。...
4. **Math.round**:Math.round 方法用于四舍五入,它会将浮点数加0.5后再进行向下取整。所以,`Math.round(-1.5)` 结果为 -1。 5. **String 类型**:String 不是 Java 的基本数据类型,而是对象,属于引用类型。...
2. **Math类的数学运算**:程序调用了Math类的多个方法,包括绝对值(`abs()`), 向上取整(`ceil()`), 向下取整(`floor()`), 指数(`exp()`), 自然对数(`log()`), 最大值(`max()`), 最小值(`min()`), 正弦(`sin()`), ...