import java.math.BigDecimal;

/**
* 处理一些数据类型的方法的java类
* @author ljb
*
*/
public class NumberTools { /**
* 根据给定的参数进行进行四舍五入
*
* @param num
* 要四舍五入的数字
* @param roundBit
* 四舍五入位数 正数表示:小数点后位数;负数表示:小数前位数
* @return 四舍五入后的数字
*/
public static double round(double num, int roundBit) {
int piontBit = 1;
double numtmp = 0.0D;
if (roundBit < 0) {
String tmpstr = "1";
roundBit = Math.abs(roundBit);
for (int i = 0; i < roundBit; i++) {
tmpstr = tmpstr + "0";
}
piontBit = Integer.parseInt(tmpstr);
roundBit = 0;
num /= piontBit;
}
BigDecimal b = new BigDecimal(Double.toString(num));
BigDecimal one = new BigDecimal("1");
numtmp = b.divide(one, roundBit, BigDecimal.ROUND_HALF_UP).doubleValue();
return numtmp * piontBit;
} /**
* 根据给定的参数进行进行四舍五入
*
* @param num
* 四舍五入的数字
* @param roundBit
* 四舍五入位数 正数表示:小数点后位数;负数表示:小数前位数
* @return 四舍五入后的数字
*/
public static String roundToStr(double num, int roundBit) {
int piontBit = 1;
double numtmp = 0.0D;
if (roundBit < 0) {
String tmpstr = "1";
roundBit = Math.abs(roundBit);
for (int i = 0; i < roundBit; i++) {
tmpstr = tmpstr + "0";
}
piontBit = Integer.parseInt(tmpstr);
roundBit = 0;
num /= piontBit;
} BigDecimal b = new BigDecimal(Double.toString(num)); BigDecimal one = new BigDecimal("1");
if (piontBit == 1) {
return b.divide(one, roundBit, BigDecimal.ROUND_HALF_UP).toString();
}
numtmp = b.divide(one, roundBit, BigDecimal.ROUND_HALF_UP).doubleValue();
return new BigDecimal(numtmp * piontBit).toString();
} // public static void main(String[] args){
// System.out.println(roundToStr(12,2));
// } }

double四舍五入,double四舍五入并转成string的更多相关文章

  1. impala 四舍五入后转换成string后又变成一个double的数值解决(除不尽的情况)

    impala 四舍五入后转换成string后又变成一个double的数值解决(除不尽的情况)例如Query: select cast(round(2 / 3, 4)*100 as string)+-- ...

  2. (后台)Java:对double值进行四舍五入,保留两位小数的几种方法

    mport java.text.DecimalFormat; DecimalFormat df = new DecimalFormat("######0.00"); double ...

  3. Java:对double值进行四舍五入,保留两位小数的几种方法

    1. 功能 将程序中的double值精确到小数点后两位.可以四舍五入,也可以直接截断. 比如:输入12345.6789,输出可以是12345.68也可以是12345.67.至于是否需要四舍五入,可以通 ...

  4. [Java]对double变量进行四舍五入,并保留小数点后位数

    1.功能 将double类型变量进行四舍五入,并保留小数点后位数 2.代码 import java.math.BigDecimal; import java.math.RoundingMode; im ...

  5. java中将double保留两位小数,将double保留两位小数并转换成String

    将Double类型的数据保留2位小数: Double a = 3.566; BigDecimal bd = new BigDecimal(a); Double d = bd.setScale(2, B ...

  6. double型转换成string型

    double型转换成string型 题目描写叙述: 如有一个函数.其可接受一个long double參数,并将參数转换为字符串.结果字符串应保留两位小数,比如,浮点值123.45678应该生成&quo ...

  7. Java,double类型转换成String,String装换成double型

    今天,老师布置了小系统,银行用户管理系统,突然发现自己的基础知识好薄弱,就把这些记录一下, double类型转化string:Double.toString(double doub); String类 ...

  8. c语言double类型数据四舍五入

    借助math库的round函数 #include <math.h> double ext_round(double data, int precision) { , precision); ...

  9. .NET向WebService传值为decimal、double、int、DateTime等非string类型属性时,服务器端接收不到数据的问题

    最近在做CRM项目时,使用C#调用SAP PI发布的WebService服务时遇到的问题: 向WebService传值为decimal.double.int.DateTime等非string类型数据时 ...

随机推荐

  1. spring3 项目更新

    列志华 (组长) http://www.cnblogs.com/liezhihua/ 团队guihub https://github.com/LWHTF/OrderingFood 黄柏堂 http:/ ...

  2. 利用nodejs的cheerio抓取网站数据

    /*引入模块*/ var http = require('http') var url = 'http://www.cnblogs.com/txxt' var cheerio = require('c ...

  3. Protocol buffers 介绍

    Protocol buffers和mxl一样在序列化数据结构时很灵活.高效和智能,但是它的优势在于定义文件更小,读取速度更快,使用更加简单.目前protocol buffers支持C++.java和p ...

  4. 如何在java中拟合正态分布

    前言 最近在工作中需要拟合高斯曲线,在python中可以使用 scipy,相关代码如下: #!/usr/bin/env python # -*- coding=utf-8 -*- %matplotli ...

  5. 微信支付之JSAPI开发-第二篇:业务流程详解与方案设计

    微信支付流程 流程: 上图的网址为:https://pay.weixin.qq.com/wiki/doc/api/jsapi.php?chapter=7_4 如上图所示,微信网页支付的具体流程大致分为 ...

  6. 把内容生成txt文件

    StringBuilder MailLog = new StringBuilder();            string logPath = txtFile + str + DateTime.No ...

  7. python学习(解析python官网会议安排)

    在学习python的过程中,做练习,解析https://www.python.org/events/python-events/ HTML文件,输出Python官网发布的会议时间.名称和地点. 对ht ...

  8. Expression Tree Basics 表达式树原理

    variable point to code variable expression tree data structure lamda expression anonymous function 原 ...

  9. 水果姐逛水果街Ⅱ codevs 3305

    3305 水果姐逛水果街Ⅱ  时间限制: 2 s  空间限制: 256000 KB   题目描述 Description 水果姐第二天心情也很不错,又来逛水果街. 突然,cgh又出现了.cgh施展了魔 ...

  10. 搭建angular2环境(1)

    1.安装node(windows环境) 进入node官网https://nodejs.org/en/下载好后直接安装就可以了.安装完成之后可以在命令窗口查看安装的版本 2.安装npm express ...