package com.sinosoft.web.controller.test.calculate; import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONObject; import com.google.common.util.concurrent.RateLimiter; import org.junit.Test; import java.math.BigDecimal; import java.math.RoundingMode; import java.text.DecimalFormat; import java.text.SimpleDateFormat; import java.util.*; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.util.concurrent.ThreadPoolExecutor; import java.util.concurrent.TimeUnit; import java.util.stream.Collectors; public class CalculateTest { /** * 四舍五入 */ @Test public void roundTest() { //方法一 double f = 3.1556; BigDecimal bigDecimal = new BigDecimal(f); double a = bigDecimal.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue(); System.out.println(a); //方法二: #.00 00表示的是小数点后两位 000表示的是小数点的后三位(貌似对于三位的四舍五入不好使) String format = new DecimalFormat("#.00").format(3.1155); System.out.println(format); // 方法三: %.2f %.表示小数点前任意位数 2 表示两位小数 格式后的结果为f 表示浮点型 double d = 3.1451; String b = String.format("%.3f", d); System.out.println(b); //方法四: double g = Math.round(5.2252 * 100) * 0.01d; System.out.println(g); } @Test public void sumTest() { List<Integer> list = new ArrayList<>(); list.add(1); list.add(2); list.add(3); list.add(4); for (int i = 0; i < list.size(); i++) { System.out.println(list.get(i)); } System.out.println("**************************"); list.remove(list.get(list.size() - 1)); for (int i = 0; i < list.size(); i++) { System.out.println(list.get(i)); } /* double sum = list.stream().mapToDouble(Integer::longValue).sum()-list.get(list.size()-1); System.out.println(sum);*/ } /** * 测试不分期计算方式 */ @Test public void calTest() { //不分期计算: double sum = 260; List<Double> list = new ArrayList<>(); list.add(50d); list.add(100d); list.add(180d); list.add(270d); List<Double> doubles = new ArrayList<>(); List<Double> temp = new ArrayList<>(); for (int i = 0; i < list.size(); i++) { doubles.add(new BigDecimal(list.get(i) / 600 * sum).setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue()); temp.add(list.get(i) / 600 * sum); if (i == list.size() - 1) { temp.remove(temp.size() - 1); double test = doubles.stream().mapToDouble(Double::doubleValue).sum() - temp.stream().mapToDouble(Double::doubleValue).sum(); doubles.set(i, test); } } for (int i = 0; i < doubles.size(); i++) { System.out.println(doubles.get(i)); } } @Test public void sort() { List<Integer> list = new ArrayList<>(); list.add(1); list.add(2); list.add(3); list.add(4); List<Integer> collect = list.stream().sorted((a, b) -> b - a).collect(Collectors.toList()); for (Integer i : collect) { System.out.println(i); } //分期计算 } @Test public void bucker() { //新建一个每秒限制3个的令牌桶 RateLimiter rateLimiter = RateLimiter.create(3.0); ThreadPoolExecutor threadPoolExecutor = (ThreadPoolExecutor) Executors.newFixedThreadPool(100); for (int i = 0; i < 10; i++) { threadPoolExecutor.execute(new Runnable() { @Override public void run() { //获取令牌桶中的一个令牌,最多等待10秒 if (rateLimiter.tryAcquire(1, 10, TimeUnit.SECONDS)) { System.out.println(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date())); } } }); } threadPoolExecutor.shutdown(); } @Test public void jackJsonTest(){ //bigdecimal的取四舍五入 BigDecimal bigDecimal = new BigDecimal(1.006); System.out.println(bigDecimal); BigDecimal decimal = bigDecimal.setScale(2,BigDecimal.ROUND_HALF_UP); System.out.println(decimal); //加减乘除 BigDecimal a = new BigDecimal(1); BigDecimal b = new BigDecimal(2); System.out.println("减法:"+a.subtract(b));//-1 System.out.println("加法:"+a.add(b));//3 System.out.println("乘法:"+a.multiply(b));//2 System.out.println("除法:"+a.divide(b));//0.2 } }
Bigdecimal的计算方式
最新推荐文章于 2023-10-13 09:25:53 发布