Java - BigDecimal
Posted yinku
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Java - BigDecimal相关的知识,希望对你有一定的参考价值。
BigDecimal和BigInteger的区别主要在于除法会除不尽,需要指定精确到小数点后多少位以及舍入方法。
import java.io.*;
import java.math.*;
import java.util.*;
public class Main {
public static void main(String[] args) {
InputStream inputStream = System.in;
OutputStream outputStream = System.out;
InputReader in = new InputReader(inputStream);
PrintWriter out = new PrintWriter(outputStream);
Solver solver = new Solver();
solver.solve(in, out);
out.close();
}
static class Solver {
public void solve(InputReader in, PrintWriter out) {
BigDecimal d1=BigDecimal.valueOf(1);
BigDecimal d2=BigDecimal.valueOf(7);
out.print(d1+"/"+d2+"=");
out.println(d1.divide(d2, 30, RoundingMode.HALF_UP));
}
}
static class InputReader {
public BufferedReader reader;
public StringTokenizer tokenizer;
public InputReader(InputStream stream) {
reader = new BufferedReader(new InputStreamReader(stream), 32768);
tokenizer = null;
}
public String next() {
while (tokenizer == null || !tokenizer.hasMoreTokens()) {
try {
tokenizer = new StringTokenizer(reader.readLine());
} catch (IOException e) {
throw new RuntimeException(e);
}
}
return tokenizer.nextToken();
}
public int nextInt() {
return Integer.parseInt(next());
}
public long nextLong() {
return Long.parseLong(next());
}
}
}
以上是关于Java - BigDecimal的主要内容,如果未能解决你的问题,请参考以下文章