Disclaimer: This article is a blogger original article, shall not be reproduced without the bloggers allowed. https://blog.csdn.net/shy415502155/article/details/88555541
A method of interception method setScale
/***
* @Description: 数字截取后几位,四舍五入
* @param: d
* @param: digit
*/
public static double round(double d, int digit) {
BigDecimal b = new BigDecimal(d);
return b.setScale(digit, BigDecimal.ROUND_HALF_UP).doubleValue();
}
Interception method using two java.text.DecimalFormat
/***
*
* @Description: 四舍五入
* @param d
* @param type 如: "#.00" 即表示为保留小数点后两位 几个0代表保留的位数
* @return
* @return double
* @throws @throws
*/
public static double subDecimalFormat(double d, String type) {
if (StringUtils.isBlank(type)) {
type = "#";
}
java.text.DecimalFormat df = new java.text.DecimalFormat(type);
double d1 = Double.parseDouble(df.format(d));
return d1;
}
The floating-point numbers into percentages
/**
* @Description: 将浮点数转为百分数
* @param d
* @param IntegerDigits 小数点前保留的位数
* @param FractionDigits 小数点后保留的位数
* @return String
* @time 2018年10月1日 上午11:09:35
*/
public static String getPercentFormat(double d,int IntegerDigits,int FractionDigits){
NumberFormat nf = java.text.NumberFormat.getPercentInstance();
nf.setMaximumIntegerDigits(IntegerDigits);//小数点前保留几位
nf.setMinimumFractionDigits(FractionDigits);// 小数点后保留几位
String str = nf.format(d);
return str;
}
The percentage converted to floating point
/**
* @Description: 将百分数转为浮点数
* @param d
* @return double
* @time 2018年10月1日 上午11:09:35
*/
public static double getPercentToDouble(String d, int digit){
NumberFormat nf=NumberFormat.getPercentInstance();
Number m = null;
try {
m = nf.parse(d);
} catch (ParseException e) {
e.printStackTrace();
}
return round(Double.parseDouble(String.valueOf(m.toString())), digit) ;
}
Test categories:
import org.apache.commons.lang3.StringUtils;
import com.shihy.springboot.utils.CommonUtils;
import lombok.extern.slf4j.Slf4j;
@Slf4j
public class ToFixedTest {
public static void main(String[] args) {
double d = 1.1234567879;
round(d, 0);
subDecimalFormat(d, "#.0000000");
doubleToPercent(d, Integer.MAX_VALUE, 1);
String str = "12.3%";
getPercentToDouble(str, 5);
}
public static void round(double d, int digit) {
double d1 = CommonUtils.round(d, digit);
log.info("四舍五入保留小数点后"+ digit + "位, 输出结果为 :" + d1);
}
public static void subDecimalFormat(double d, String type) {
double d1 = CommonUtils.subDecimalFormat(d, type);
if (StringUtils.isBlank(type)) {
type = "#.0000000";
}
log.info("四舍五入保留小数点按照" + type +"格式, 输出结果为 :" + d1);
}
public static void doubleToPercent(double d, int integerDigits, int fractionDigits) {
String percent = CommonUtils.getPercentFormat(d, integerDigits, fractionDigits);
log.info("浮点转为百分数, 输出结果为 :" + percent);
}
public static void getPercentToDouble(String d, int digit) {
double d1 = CommonUtils.getPercentToDouble(d, digit);
log.info("百分数转为浮点,保留小数点后"+ digit +"位, 输出结果为 :" + d1);
}
}
result:
16:17:39.136 - 四舍五入保留小数点后0位, 输出结果为 :1.0
16:17:39.145 - 四舍五入保留小数点按照#.0000000格式, 输出结果为 :1.1234568
16:17:39.145 - 浮点转为百分数, 输出结果为 :112.3%
16:17:39.145 - 百分数转为浮点,保留小数点后5位, 输出结果为 :0.123