初次接触BigDecimal()

java math中的BigDecimal()

BigDecimal类

一般的Float类和Double类可以用来做科学计算或工程计算,但在商业计算中,要求数字精度比较高,故用到java.math.BigDecimal类。BigDecimal类支持任何精度的定点数。

构造器

public BigDecimal(double val)

public BigDecimal(String val)

常用方法

public BigDecimal add(BigDecimal augend)

public BigDecimal subtract(BigDecimal subtrahend)

public BigDecimal multiply(BigDecimal multiplicand)

public BigDecimal divide(BigDecimal divisor, int scale, int roundingMode)

同样还有

BigInteger类

Integer类作为int的包装类,能存储的最大整型值为2^31−1,BigInteger类的数字范围较Integer类的数字范围要大得多,可以支持任意精度的整数

BigInteger(String val)

- 常用方法

* public BigInteger abs()

* public BigInteger add(BigInteger val)

* public BigInteger subtract(BigInteger val)

* public BigInteger multiply(BigInteger val)

* public BigInteger divide(BigInteger val)

* public BigInteger remainder(BigInteger val)

* public BigInteger pow(int exponent)

* public BigInteger[] divideAndRemainder(BigInteger val)

例题(选自杭电acm2054)

A == B ?

Problem Description

Give you two numbers A and B, if A is equal to B, you should print "YES", or print "NO".

Input

each test case contains two numbers A and B.

Output

for each case, if A is equal to B, you should print "YES", or print "NO".

Sample Input

1 2

2 2

3 3

4 3

Sample Output

NO

YES

YES

NO

代码:package acm题目;


import java.math.BigDecimal;

import java.util.Scanner;


public class acm2054 {


public static void main(String[] args) {

  Scanner sc=new Scanner(System.in);

  while(sc.hasNext()) {

  BigDecimal A=sc.nextBigDecimal();

  BigDecimal B=sc.nextBigDecimal();

  if(A.compareTo(B)==0)

    System.out.println("YES");

  else System.out.println("NO");

  }

}

}

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • BigInteger类 BigInteger类概述可以让超过Integer范围内的数据进行运算 构造方法publi...
    清风沐沐阅读 1,230评论 0 3
  • [TOC] 正则表达式 概述及基本使用 正则表达式:是指用来描述或者匹配一系列符合某个句法规则的字符串的单个字符串...
    lutianfei阅读 398评论 0 2
  • K# Java核心技术卷一 逐一声明每一个变量可以提高程序的可读性。 在Java中,变量的声明尽可能地靠近变量第一...
    兰陵忧患生阅读 460评论 0 1
  • 选择题部分 1.()部门负责日常监督检查工作,安全巡视的同时进行消防检查,推动消防安全制度的贯彻落实。 A: 消防...
    skystarwuwei阅读 15,723评论 0 3
  • 选择题部分 1.(),只有在发生短路事故时或者在负荷电流较大时,变流器中才会有足够的二次电流作为继电保护跳闸之用。...
    skystarwuwei阅读 13,747评论 0 7