全城热恋20131208,uyttt,卉组词
本文实例为大家分享了java指定精确小数位的具体代码,供大家参考,具体内容如下
java代码:
public class app2 { public static void main(string[] args) { string val = checknumber("10.1234155", 2, 6).tostring(); system.out.println(val); } public static bigdecimal checknumber(string number, int min, int max) { // 初始化为max位小数 decimalformat df = new decimalformat(getmaxstr(max)); string num = df.format(double.parsedouble(number)); bigdecimal bd = new bigdecimal(num); string val = bd.tostring(); val = val.replaceall("^(0+)", ""); val = val.replaceall("(0+)$", ""); int idx = val.indexof("."); int len = val.substring(idx + 1).length(); if (len < min) { if (len == 0 && idx == 0) { bd = new bigdecimal("0.00"); } else { bd = new bigdecimal(val).setscale(2); } } else { bd = new bigdecimal(val).setscale(len); } return bd; } public static string getmaxstr(int max) { string val = "0."; for (int i = 0; i < max; i++) { val += "0"; } return val; } }
控制台:
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持移动技术网。
如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复
浅析我对 String、StringBuilder、StringBuffer 的理解
使用IDEA搭建SSM框架的详细教程(spring + springMVC +MyBatis)
Springboot整合freemarker 404问题解决方案
引入mybatis-plus报 Invalid bound statement错误问题的解决方法
网友评论