• double 类型怎样不用科学计数法表示并且使用Java正则表达式去掉Double类型的数据后面多余的0


    关于Double变量转字符串(不用科学计数法表示)的方法。

    Double类型的变量在区间(负一千万,正一千万)中是正常显示的, 无小数部分时默认带一位小数,如:

    9999999—>9999999.0

    10000000—>1.0E7

    10000000.1—>1.00000001E7

    想让Double变量无论是什么数值都不使用科学计数法表示,那么只能将此值转换为字符串形式,当然也不是直接用String.valueOf(Double target)来转换,而是采用以下的方法:

    1. 先将Double类型转换成String类型
    2. 再将String类型转换成BigDecimal类型
    3. 再将BigDecimal类型转换成String

    注意!!!

    不能直接将Double类型转换成BigDecimal类型,否则会因为精度问题造成数值偏差。

    一定要按照Double—>String—>BigDecimal—>String路线走。

    下面是示例代码:

    //创建Double变量target并完成初始化
    Double target = new Double(10000000.1);
    
    //将Double变量target转换成字符串temporary
    String temporary = String.valueOf(target);
    
    //将字符串temporary转换成BigDecimal变量bigDecimal
    BigDecimal bigDecimal = new BigDecimal(temporary);
    
    //将BigDecimal变量bigDecimal转换成字符串result
    String result = String.valueOf(bigDecimal);
    

    最后来一个小题目:如何判断Double类型变量的值中的小数部分超过两位?

    思路:

    1. 先将Double变量转换成String变量(不用科学计数法表示)
    2. 将该String变量以小数点进行分割,并将分割后的各部分用数组保存起来
    3. 对数组进行相应的判断,即可得到结果。

    话不多说,直接上代码。

    //创建Double变量target并完成初始化
    Double target = new Double(10000000.111);
    
    //将Double变量target转换成字符串temporary
    String temporary = String.valueOf(target);
    
    //将字符串temporary转换成BigDecimal变量bigDecimal
    BigDecimal bigDecimal = new BigDecimal(temporary);
    
    //将BigDecimal变量bigDecimal转换成字符串result
    String result = String.valueOf(bigDecimal);
    
    //将String变量以小数点进行分割,并将分割后的各部分用数组保存起来,注意小数点符号需要转义处理
    String[] judgeArray = result.split("\.");
    
    //对该字符串数组进行判断,若数组含有超过一个元素,并且第二个元素的长度大于2,则表明该Double类型变量的值中的小数部分超过两位
    if (judgeArray.length > 1 && judgeArray[1].length() > 2) {
    	System.out.println("该Double类型变量的值中的小数部分超过两位");
    }
    

    关于String.split(String regex)方法,值得注意的是:

    在以“|”或者“.”或者“+”或者“*”进行分割时,要对它们进行转义处理,即要使用String.split("\.")String.split("\|")String.split("\+")String.split("\*")等进行分割,不能使用String.split(".")String.split("|")String.split("+")String.split("*")等进行分割。

     
    //创建Double变量target并完成初始化
    Double target = new Double(10000000.1);
    
    //将Double变量target转换成字符串temporary
    String temporary = String.valueOf(target);
    
    //将字符串temporary转换成BigDecimal变量bigDecimal
    BigDecimal bigDecimal = new BigDecimal(temporary);
    
    //将BigDecimal变量bigDecimal转换成字符串result
    String result = String.valueOf(bigDecimal);




    /** * 使用java正则表达式去掉多余的.与0 * @param s * @return */ public static String subZeroAndDot(String s){ if(s.indexOf(".") > 0){ s = s.replaceAll("0+?$", "");//去掉多余的0 s = s.replaceAll("[.]$", "");//如最后一位是.则去掉 } return s;




    System.out.println(subZeroAndDot("1"));     // 转换后为1
    System.out.println(subZeroAndDot("10"));    // 转换后为10
    System.out.println(subZeroAndDot("1.0"));   // 转换后为1
    System.out.println(subZeroAndDot("1.010")); // 转换后为1.01 
    System.out.println(subZeroAndDot("1.01"));  // 转换后为1.01


    整理自:https://www.cnblogs.com/sesexxoo/p/6190517.html
    https://bbs.csdn.net/topics/40170929
  • 相关阅读:
    python实现git操作
    CentOS 安装R包 报错:internet routines cannot be loaded ,如何解决
    CentOS 6.8上安装指定版本的注释软件 VEP,release 93.4
    Anaconda3安装后在Pycharm中报错:ImportError: DLL load failed: 找不到指定的模块
    CentOS编译fastp 0.19.4 时报错/usr/bin/ld: cannot find -lz
    推荐一款非常好用的在线python编辑器——Colaboratory
    mac如何用quick look预览多个文件或者图片
    Pileup 格式详细说明
    CentOS 7 上安装 Django 2.2.4,解决报错:No module named ‘_sqlite3′
    Message: 'chromedriver' executable needs to be available in the path.
  • 原文地址:https://www.cnblogs.com/yangsanluo/p/14435068.html
Copyright © 2020-2023  润新知