模式切换
数字处理类
Java 提供了多个类来处理数字,包括数字格式化、数学运算、随机数生成以及大数字计算等功能。常用的数字处理类包括 Math
、Random
、BigInteger
和 BigDecimal
等。
数字格式化
Java 提供了 DecimalFormat
、NumberFormat
等类来格式化数字,使其符合特定的格式要求。
DecimalFormat 示例
java
import java.text.DecimalFormat;
public class DecimalFormatExample {
public static void main(String[] args) {
DecimalFormat df = new DecimalFormat("#,###.00");
double num = 12345.678;
System.out.println("格式化后: " + df.format(num)); // 12,345.68
}
}
NumberFormat 示例
java
import java.text.NumberFormat;
import java.util.Locale;
public class NumberFormatExample {
public static void main(String[] args) {
double value = 12345.678;
NumberFormat nf = NumberFormat.getCurrencyInstance(Locale.US);
System.out.println("美元格式: " + nf.format(value)); // $12,345.68
}
}
数学运算
Math 类
Java 的 Math
类提供了基本的数学运算方法,所有方法都是 static
静态方法,无需创建对象即可使用。
常用的数学运算方法
方法 | 说明 |
---|---|
Math.abs(x) | 绝对值 |
Math.ceil(x) | 向上取整 |
Math.floor(x) | 向下取整 |
Math.round(x) | 四舍五入 |
Math.max(a, b) | 取最大值 |
Math.min(a, b) | 取最小值 |
Math.pow(a, b) | 计算 a 的 b 次幂 |
Math.sqrt(x) | 计算平方根 |
Math.log(x) | 计算自然对数 |
Math.sin(x) / Math.cos(x) / Math.tan(x) | 三角函数 |
示例:
java
public class MathExample {
public static void main(String[] args) {
System.out.println("绝对值: " + Math.abs(-10)); // 10
System.out.println("向上取整: " + Math.ceil(3.14)); // 4.0
System.out.println("向下取整: " + Math.floor(3.99));// 3.0
System.out.println("四舍五入: " + Math.round(4.5)); // 5
System.out.println("2^3: " + Math.pow(2, 3)); // 8.0
System.out.println("平方根: " + Math.sqrt(16)); // 4.0
}
}
随机数
Math.random() 方法
Math.random()
生成 [0.0, 1.0)
之间的随机数。
java
public class RandomExample {
public static void main(String[] args) {
double randomNum = Math.random(); // [0.0, 1.0)
System.out.println("随机数: " + randomNum);
int randomInt = (int) (Math.random() * 100); // 0-99
System.out.println("0-99 之间的随机整数: " + randomInt);
}
}
Random 类
Java 提供 Random
类来生成随机数,并支持整数、浮点数、布尔值等类型。
示例:
java
import java.util.Random;
public class RandomClassExample {
public static void main(String[] args) {
Random random = new Random();
System.out.println("随机整数: " + random.nextInt(100)); // 0-99
System.out.println("随机浮点数: " + random.nextDouble()); // [0.0, 1.0)
System.out.println("随机布尔值: " + random.nextBoolean()); // true or false
}
}
大数字运算
在 Java 中,int
和 long
不能处理超大数值,Java 提供了 BigInteger
和 BigDecimal
进行大数计算。
BigInteger(大整数运算)
适用于超大整数计算,支持四则运算、取模、位运算等。
示例:
java
import java.math.BigInteger;
public class BigIntegerExample {
public static void main(String[] args) {
BigInteger a = new BigInteger("987654321987654321");
BigInteger b = new BigInteger("123456789123456789");
System.out.println("加法: " + a.add(b));
System.out.println("乘法: " + a.multiply(b));
System.out.println("取模: " + a.mod(new BigInteger("1000")));
}
}
BigDecimal(高精度浮点运算)
BigDecimal
主要用于金融计算,避免浮点运算误差。
示例:
java
import java.math.BigDecimal;
public class BigDecimalExample {
public static void main(String[] args) {
BigDecimal num1 = new BigDecimal("0.1");
BigDecimal num2 = new BigDecimal("0.2");
System.out.println("加法: " + num1.add(num2));
System.out.println("乘法: " + num1.multiply(num2));
System.out.println("除法: " + num1.divide(new BigDecimal("0.3"), 2, BigDecimal.ROUND_HALF_UP));
}
}