Laravel  
laravel
文档
数据库
架构
入门
php技术
    
Laravelphp
laravel / php / java / vue / mysql / linux / python / javascript / html / css / c++ / c#

java biginteger

作者:灭世杀   发布日期:2025-04-24   浏览:89

import java.math.BigInteger;

public class BigIntegerExample {
    public static void main(String[] args) {
        // 创建两个BigInteger对象
        BigInteger num1 = new BigInteger("123456789012345678901234567890");
        BigInteger num2 = new BigInteger("987654321098765432109876543210");

        // 加法操作
        BigInteger sum = num1.add(num2);
        System.out.println("num1 + num2 = " + sum);

        // 减法操作
        BigInteger difference = num1.subtract(num2);
        System.out.println("num1 - num2 = " + difference);

        // 乘法操作
        BigInteger product = num1.multiply(num2);
        System.out.println("num1 * num2 = " + product);

        // 除法操作
        BigInteger quotient = num1.divide(num2);
        System.out.println("num1 / num2 = " + quotient);

        // 取模操作
        BigInteger remainder = num1.mod(num2);
        System.out.println("num1 % num2 = " + remainder);

        // 比较大小
        int comparison = num1.compareTo(num2);
        if (comparison > 0) {
            System.out.println("num1 is greater than num2");
        } else if (comparison < 0) {
            System.out.println("num1 is less than num2");
        } else {
            System.out.println("num1 is equal to num2");
        }
    }
}

解释说明:

  • BigInteger 是 Java 中的一个类,用于表示任意精度的整数。它可以处理超出基本数据类型(如 intlong)范围的数值。
  • new BigInteger(String val):通过字符串创建一个 BigInteger 对象。
  • add(BigInteger val):返回两个 BigInteger 的和。
  • subtract(BigInteger val):返回两个 BigInteger 的差。
  • multiply(BigInteger val):返回两个 BigInteger 的积。
  • divide(BigInteger val):返回两个 BigInteger 的商。
  • mod(BigInteger val):返回两个 BigInteger 的余数。
  • compareTo(BigInteger val):比较两个 BigInteger 的大小,返回值为 -101,分别表示小于、等于或大于。

上一篇:java date 转 localdatetime

下一篇:java @override的作用

大家都在看

java连接数据库的代码

ubuntu 卸载java

java sort用法

java collections.sort

java file类的方法

java 判断

java时间数据类型

java 时间加一天

java demo

java 截取

Laravel PHP 深圳智简公司。版权所有©2023-2043 LaravelPHP 粤ICP备2021048745号-3

Laravel 中文站