在Java中,我想扫描用户的formatString和inputDouble,如下所示:
Scanner scan = new Scanner(System.in);
System.out.println("Enter formating string and double to format:");
String formatString = scan.next();
Double inputDouble = scan.nextDouble();
但是现在,假设我只有
String formatString = "%.2f";
Double inputDouble = 42.424242;
我会使用 System.out.printf();根据 formatString 格式化 inputDouble。像这样的东西:
System.out.printf("Formated double: '%s'", formatString, inputDouble);
所以形式上的东西:
System.out.printf("Formated double: 'magicGoesHere'", formatString, inputDouble);
所以在这种特殊情况下,我想看到输出:
Formated double: 42.42
有可能吗?谢谢
最佳答案
System.out.printf("Formated double: '%s'", String.format (formatString, inputDouble));
关于java printf 格式带有字符串参数的 double ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33534269/