惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

GbyAI
GbyAI
J
Java Code Geeks
雷峰网
雷峰网
WordPress大学
WordPress大学
宝玉的分享
宝玉的分享
云风的 BLOG
云风的 BLOG
V
Visual Studio Blog
V
Vulnerabilities – Threatpost
S
Securelist
The Hacker News
The Hacker News
The Register - Security
The Register - Security
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
Help Net Security
Help Net Security
G
Google Developers Blog
Hugging Face - Blog
Hugging Face - Blog
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
M
MIT News - Artificial intelligence
AI
AI
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
The GitHub Blog
The GitHub Blog
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
Schneier on Security
Schneier on Security
N
Netflix TechBlog - Medium
T
The Blog of Author Tim Ferriss
Google DeepMind News
Google DeepMind News
Hacker News - Newest:
Hacker News - Newest: "LLM"
H
Hacker News: Front Page
博客园 - 司徒正美
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
B
Blog
Microsoft Azure Blog
Microsoft Azure Blog
大猫的无限游戏
大猫的无限游戏
Security Latest
Security Latest
Engineering at Meta
Engineering at Meta
N
News and Events Feed by Topic
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
酷 壳 – CoolShell
酷 壳 – CoolShell
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
T
Threat Research - Cisco Blogs
U
Unit 42
V
V2EX
V2EX - 技术
V2EX - 技术
L
LINUX DO - 最新话题
aimingoo的专栏
aimingoo的专栏
Microsoft Security Blog
Microsoft Security Blog
Recorded Future
Recorded Future
P
Privacy & Cybersecurity Law Blog
美团技术团队
小众软件
小众软件
F
Fortinet All Blogs

博客园 - 李超@hicc

r与java整合(转) SVG路径字符串格式 java统计代码小工具 google fonts ie9下对html5的修复 js和jquery求table的列和 xmlHttpRequest的五种状态 JAVA实现逆波兰转换 顺排文档中逻辑树展开法的部分代码实现 页面且换时的效果 - 李超@hicc - 博客园 在线生成AJAX素材图片 将汉字转化为汉语拼音的小工具 无聊了,恶搞一下,程序员每天必须开的页面 防盗链原理 利用commonsfileupload+ffmpeg+mencoder完成视频的上传与转换(2) JAVA上传文件进度条的实现 通过jdbc 连接 Access - 李超@hicc Struts2&Jquery 新闻发布 使用AjaxTags实现自动完成
JAVA实现精确的加减乘除
李超@hicc · 2009-05-20 · via 博客园 - 李超@hicc

package lc.util;

import java.math.BigDecimal;

public class MathHelper {
 private static final int DEF_DIV_SCALE = 10;

 private MathHelper() {

 }

 /**
  * 提供精确的加法运算。
  *
  * @param v1
  *            被加数
  * @param v2
  *            加数
  * @return 两个参数的和
  */
 public static double add(double v1, double v2) {
  BigDecimal b1 = new BigDecimal(Double.toString(v1));
  BigDecimal b2 = new BigDecimal(Double.toString(v2));
  return b1.add(b2).doubleValue();
 }

 /**
  * 提供精确的减法运算。
  *
  * @param v1
  *            被减数
  * @param v2
  *            减数
  * @return 两个参数的差
  */
 public static double sub(double v1, double v2) {
  BigDecimal b1 = new BigDecimal(Double.toString(v1));
  BigDecimal b2 = new BigDecimal(Double.toString(v2));
  return b1.subtract(b2).doubleValue();
 }

 /**
  * 提供精确的乘法运算。
  *
  * @param v1
  *            被乘数
  * @param v2
  *            乘数
  * @return 两个参数的积
  */
 public static double mul(double v1, double v2) {
  BigDecimal b1 = new BigDecimal(Double.toString(v1));
  BigDecimal b2 = new BigDecimal(Double.toString(v2));
  return b1.multiply(b2).doubleValue();
 }

 /**
  * 提供(相对)精确的除法运算,当发生除不尽的情况时,精确到 小数点以后10位,以后的数字四舍五入。
  *
  * @param v1
  *            被除数
  * @param v2
  *            除数
  * @return 两个参数的商
  */
 public static double div(double v1, double v2) {
  return div(v1, v2, DEF_DIV_SCALE);
 }

 /**
  * 提供(相对)精确的除法运算。当发生除不尽的情况时,由scale参数指 定精度,以后的数字四舍五入。
  *
  * @param v1
  *            被除数
  * @param v2
  *            除数
  * @param scale
  *            表示表示需要精确到小数点以后几位。
  * @return 两个参数的商
  */

 public static double div(double v1, double v2, int scale) {
  if (scale < 0) {
   throw new IllegalArgumentException(
     "The scale must be a positive integer or zero");
  }
  BigDecimal b1 = new BigDecimal(Double.toString(v1));
  BigDecimal b2 = new BigDecimal(Double.toString(v2));
  return b1.divide(b2, scale, BigDecimal.ROUND_HALF_UP).doubleValue();
 }

 /**
  * 提供精确的小数位四舍五入处理。
  *
  * @param v
  *            需要四舍五入的数字
  * @param scale
  *            小数点后保留几位
  * @return 四舍五入后的结果
  */
 public static double round(double v, int scale) {
  if (scale < 0) {
   throw new IllegalArgumentException(
     "The scale must be a positive integer or zero");
  }
  BigDecimal b = new BigDecimal(Double.toString(v));
  BigDecimal one = new BigDecimal("1");
  return b.divide(one, scale, BigDecimal.ROUND_HALF_UP).doubleValue();
 }
}