使用工具类-java精确到小数点后6位
作者:阿拉的梦想 时间:2021-06-24 20:45:39
工具类-java精确到小数点后6位
验证要求,必须精确到小数点后6位,但是后面都是0的话,double会省略0,正则验证不通过,所以
有了下面解决方法
精确到小数点后六位,若最后一位是0则用1代替。
private double getSixScale(String s) {
? ? //先精确到后6位,并转为字符串
? ? String sixScaleString = new BigDecimal(s).setScale(6, BigDecimal.ROUND_HALF_UP).toString();
? ? //若字符串最后一位是0,则用1代替
? ? if ("0".equals(sixScaleString.substring(sixScaleString.length() - 1))) {
? ? ? ? sixScaleString = sixScaleString.substring(0, sixScaleString.length() - 1) + "1";
? ? }
? ? //再转为double
? ? return new BigDecimal(sixScaleString).doubleValue();
}
java除法运算问题(保留小数)
编程的人都知道,java中的“/”、“%”运算,其中前者为取整,后者取余数。
那么有没有快捷的运算方法取正常的运算结果呢?
查了资料,发现很简单。
代码如下:
/**
* TODO 除法运算,保留小数
* @author 袁忠明
* @date 2018-4-17下午2:24:48
* @param a 被除数
* @param b 除数
* @return 商
*/
public static String txfloat(int a,int b) {
// TODO 自动生成的方法存根
DecimalFormat df=new DecimalFormat("0.00");//设置保留位数
return df.format((float)a/b);
}
来源:https://blog.csdn.net/A434534658/article/details/118543635
标签:java,小数点,6位
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
C#中winform控制textbox输入只能为数字的方法
2023-06-26 12:09:03
C#使用有道ip地址查询接口方法实例详解
2022-10-08 22:07:31
WebView的介绍与简单实现Android和H5互调的方法
2023-04-30 19:26:00
![](https://img.aspxhome.com/file/2023/7/101817_0s.png)
基于Java实现多线程下载并允许断点续传
2021-07-19 03:05:52
![](https://img.aspxhome.com/file/2023/3/101783_0s.png)
springboot快速集成mybatis-plus的详细教程
2021-06-11 22:05:16
![](https://img.aspxhome.com/file/2023/4/73014_0s.png)
spring data JPA 中的多属性排序方式
2023-01-14 19:26:41
Java程序中实现调用Python脚本的方法详解
2021-07-08 22:00:08
C#限速下载网络文件的方法实例
2023-07-01 01:33:15
![](https://img.aspxhome.com/file/2023/3/104323_0s.jpg)
聊聊Spring AOP @Before @Around @After等advice的执行顺序
2022-10-27 18:14:20
![](https://img.aspxhome.com/file/2023/3/86853_0s.jpg)
Unity3d实现无限循环滚动背景
2022-11-02 05:04:22
解决@JsonInclude(JsonInclude.Include.NON_NULL)不起作用问题
2023-05-26 08:57:45
![](https://img.aspxhome.com/file/2023/0/67470_0s.png)
Java Spring的refresh方法你知道吗
2023-07-08 11:53:18
![](https://img.aspxhome.com/file/2023/5/67095_0s.png)
Java手动实现Redis的LRU缓存机制
2023-07-31 12:51:30
![](https://img.aspxhome.com/file/2023/7/57807_0s.png)
详解Java编程中线程同步以及定时启动线程的方法
2021-08-31 10:59:20
SpringMVC+MyBatis 事务管理(实例)
2022-07-30 11:04:40
![](https://img.aspxhome.com/file/2023/9/65579_0s.png)
spring-redis-session 自定义 key 和过期时间
2022-03-29 14:34:37
java创建线程的两种方法区别
2023-11-11 09:17:52
Intellij IDEA 阅读源码的 4 个绝技(必看)
2023-06-14 11:23:16
![](https://img.aspxhome.com/file/2023/9/106109_0s.png)
java中重载,继承,重写和多态的区别
2022-05-06 06:40:16
C#实现多线程下载文件的方法
2022-04-24 10:59:43