下面是我的程序

public class HelloWorld
{
  // arguments are passed using the text field below this editor
  public static void main(String[] args)
  {
   //Scanner scanner = new Scanner(System.in);
    //String str = scanner.nextLine();
    String str=args[0];
    try{
        BigDecimal dec=new BigDecimal(str);
      System.out.println(Double.parseDouble(str)+"\t"+str);

        if(dec.intValue()==dec.doubleValue()){
            System.out.println("This input is of type Integer.");
        }else{
            System.out.println("This input is of type Float.");
        }

    }catch(Exception e){
        System.out.println("This input is of type string.");
    }

   }
}


我想将float值作为命令行参数传递。我将值0.00057作为参数传递。但是它已经转换为000057。有人可以建议我吗。

最佳答案

我认为从字符串转换为数字时出了点问题,以确保定义十进制分隔符有效:

   DecimalFormat df = new DecimalFormat();
   DecimalFormatSymbols symbols = new DecimalFormatSymbols();
   symbols.setDecimalSeparator('.');
   df.setDecimalFormatSymbols(symbols);
   Number n = df.parse(str);

   System.out.println(Double.parseDouble(str)+"\t"+str);

   if(n.intValue()==n.doubleValue()){
       System.out.println("This input is of type Integer.");
   }else{
       System.out.println("This input is of type Float.");
   }

关于java - 如何在Java程序中将double值作为命令行参数传递,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/53692552/

10-10 19:37