bigdecimal正确用法_Java BigDecimal min()用法及代码示例

Java中的java.math.BigDecimal.min(BigDecimal val)方法用于比较两个BigDecimal值并返回两者的最小值。

用法:

public BigDecimal min(BigDecimal val)

参数:该函数接受BigDecimal对象val作为参数,将其值与此BigDecimal对象的值进行比较,并返回最小值。

返回值:此方法返回BigDecimal,其值是此BigDecimal和val中的较小值。如果两者相等,则返回此BigDecimal。

例子:

Input : a = 17.000041900, b = 17.0000418999

Output : 17.0000418999

Input : a = 235900000146, b = 236000000000

Output : 235900000146

下面的程序将说明BigDecimal类的min()函数:

示例1:

/*Java program to illustrate

use of BigDecimal min()

function in Java      */

import java.math.*;

public class GFG {

public static void main(String[] args)

{

// Creating 2 BigDecimal objects

BigDecimal a, b;

a = new BigDecimal("17.000041900");

b = new BigDecimal("17.0000418999");

// print the maximum value

System.out.println("Minimum Value among " + a +

" and " + b + " is " + a.min(b));

}

}

输出:

Minimum Value among 17.000041900 and 17.0000418999 is 17.0000418999

示例2:

/*Java program to illustrate

use of BigDecimal min()

to display minimum length

among two strings  */

import java.math.*;

public class GFG {

public static void main(String[] args)

{

// Create 2 BigDecimal objects

BigDecimal a, b;

String s = "GeeksforGeeks";

String str = "GeeksClasses";

int l1, l2;

l1 = s.length();

l2 = str.length();

a = new BigDecimal(l1);

b = new BigDecimal(l2);

// Print the respective lengths

System.out.println("Length of string " + s + " is " + a);

System.out.println("Length of string " + str + " is " + b);

// Print the maximum value

System.out.println("Minimum length is " + a.min(b));

}

}

输出:

Length of string GeeksforGeeks is 13

Length of string GeeksClasses is 12

Minimum length is 12

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
本版中只有一个用于生产环境的文件:BigDecimal-all-last.min.js,大小为26K,如果WEB服务器打开GZIP压缩,可以减小至7K,如需完整版本请移步至:http://download.csdn.net/detail/aquaqu2009/4575230 简单用法: var scale = 20; var e = new BigDecimal("0"); var denominator = new BigDecimal("1"); var eBefore = new BigDecimal("-1"); var iteration = 1; while (!e.equals(eBefore)) { eBefore = e; e = e.add(BigDecimal.prototype.ONE.divide(denominator, scale, BigDecimal.prototype.ROUND_HALF_UP)); denominator = denominator.multiply(new BigDecimal(iteration+'')); ++iteration; } equal(e.toString(), "2.71828182845904523536"); ok(new BigDecimal("1234.5678").isPositive(), "1234.5678 is positive"); ok(new BigDecimal("-1234.5678").isNegative(), "-1234.5678 is negative"); ok(!BigDecimal.prototype.ZERO.isPositive() && !BigDecimal.prototype.ZERO.isNegative(), "BigDecimal.prototype.ZERO is neither positive nor negative"); ok(BigDecimal.prototype.ZERO.isZero(), "BigDecimal.prototype.ZERO is zero"); ok(new BigDecimal("1234.0000").isLessThan(new BigDecimal("1234.5678")) && new BigDecimal("1234.0000").isLessThanOrEqualTo(new BigDecimal("1234.5678")), "1234.0000 is less than 1234.5678"); ok(!new BigDecimal("1234.0000").isGreaterThan(new BigDecimal("1234.5678")) && !new BigDecimal("1234.0000").isGreaterThanOrEqualTo(new BigDecimal("1234.5678")), "1234.0000 is not greater than 1234.5678"); ok(new BigDecimal("1234.5678").isGreaterThan(new BigDecimal("1234.0000")) && new BigDecimal("1234.5678").isGreaterThanOrEqualTo(new BigDecimal("1234.0000")), "1234.5678 is greater than 1234.0000"); ok(!new BigDecimal("1234.5678").isLessThan(new BigDecimal("1234.0000")) && !new BigDecimal("1234.5678").isLessThanOrEqualTo(new BigDecimal("1234.0000")), "1234.5678 is not less than 1234.0000"); ok(new BigDecimal("1234.5678").isLessThanOrEqualTo(new BigDecimal("1234.5678")) && new BigDecimal("1234.5678").isGreaterThanOrEqualTo(new BigDecimal("1234.5678")), "1234.5678 is less than or equal to itself as well as greater than or equal to itself"); 别整个复制粘贴啊,那样是运行不起来的,请粘贴您需要的代码,,

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值