分析双java时出错

分析双java时出错,java,string,selenium,double,Java,String,Selenium,Double,我有以下代码: public void refreshResources(){ try{ String res = driver.findElement(By.cssSelector("#resources_metal")).getText(); System.out.println("Metal read: " + res); res = res.replaceAll("\\u002e", ""); //Removes dots

我有以下代码:

public void refreshResources(){
    try{
        String res = driver.findElement(By.cssSelector("#resources_metal")).getText();
        System.out.println("Metal read: " + res);
        res = res.replaceAll("\\u002e", ""); //Removes dots
        System.out.println("Metal without dots: " + res);
        this.metRes = Double.parseDouble(res);
        System.out.println("Converted Metal: " + metRes);
    }catch(NoSuchElementException error){
        System.out.println("Error please try again");
    } 
这是我的输出:

金属读数:47.386.578

无圆点金属:47386578

转换金属:4.7386578E7

问题是,为什么我的转换金属末端有“E7”? 提前感谢。

其中“x”是数字的“Ex”表示指数。因此,在您的情况下,数字“4.7386578E7”将是“47386578”。就在7号点右转

但是,如果要打印没有指数符号的数字,可以在最后一次打印中使用“printf”,就像下面的代码一样:

public void refreshResources(){
try{
    String res = driver.findElement(By.cssSelector("#resources_metal")).getText();
    System.out.println("Metal read: " + res);
    res = res.replaceAll("\\u002e", ""); //Removes dots
    System.out.println("Metal without dots: " + res);
    this.metRes = Double.parseDouble(res);
    System.out.printf("Converted Metal: %.0f", metRes);
}catch(NoSuchElementException error){
    System.out.println("Error please try again");
}
%.0f表示要打印不带小数部分的浮点值

我希望这个答案能对你有所帮助。

其中“x”是一个数字的“Ex”表示指数。因此,在您的情况下,数字“4.7386578E7”将是“47386578”。就在7号点右转

但是,如果要打印没有指数符号的数字,可以在最后一次打印中使用“printf”,就像下面的代码一样:

public void refreshResources(){
try{
    String res = driver.findElement(By.cssSelector("#resources_metal")).getText();
    System.out.println("Metal read: " + res);
    res = res.replaceAll("\\u002e", ""); //Removes dots
    System.out.println("Metal without dots: " + res);
    this.metRes = Double.parseDouble(res);
    System.out.printf("Converted Metal: %.0f", metRes);
}catch(NoSuchElementException error){
    System.out.println("Error please try again");
}
%.0f表示要打印不带小数部分的浮点值


我希望这个答案对您有所帮助。

使用“java.math”包中的“BigDecimal”代替原始的double或float。这将为您提供更精确的输出。请参阅下面使用的代码片段

try{
    String res = driver.findElement(By.cssSelector("#resources_metal")).getText();
    System.out.println("Metal read: " + res);
    res = res.replaceAll("\\u002e", ""); //Removes dots
    System.out.println("Metal without dots: " + res);
    this.metRes = Double.parseDouble(res);
    System.out.printf("Converted Metal: %.0f", metRes);
    BigDecimal bigDecimal = new BigDecimal(res);
    System.out.println("Big Decimal : "+bigDecimal);
}catch(NoSuchElementException error){
    System.out.println("Error please try again");
}

使用“java.math”包中的“BigDecimal”代替原语double或float。这将为您提供更精确的输出。请参阅下面使用的代码片段

try{
    String res = driver.findElement(By.cssSelector("#resources_metal")).getText();
    System.out.println("Metal read: " + res);
    res = res.replaceAll("\\u002e", ""); //Removes dots
    System.out.println("Metal without dots: " + res);
    this.metRes = Double.parseDouble(res);
    System.out.printf("Converted Metal: %.0f", metRes);
    BigDecimal bigDecimal = new BigDecimal(res);
    System.out.println("Big Decimal : "+bigDecimal);
}catch(NoSuchElementException error){
    System.out.println("Error please try again");
}

假设你提到这个,一个双精度函数并没有无限的精度。这就是你在科学记数法中的价值。这将给你很多关于“为什么”问题的背景知识:说真的,伙计们,非常感谢你们!假设你提到这个,一个双精度函数并没有无限的精度。这就是你在科学记数法中的价值。这将给你很多关于“为什么”问题的背景知识:说真的,伙计们,非常感谢你们!