码迷,mamicode.com
首页 > 编程语言 > 详细

Java - BigDecimal

时间:2019-04-24 00:06:13      阅读:142      评论:0      收藏:0      [点我收藏+]

标签:static   cep   NPU   next   out   ide   tokenizer   区别   round   

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

标签:static   cep   NPU   next   out   ide   tokenizer   区别   round   

原文地址:https://www.cnblogs.com/Yinku/p/10759793.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!