在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
有可能吗?感谢
答案 0 :(得分:1)
将通话分成几部分:
System.out.print("Formated double: '");
System.out.printf(formatString, inputDouble);
System.out.print("'");
答案 1 :(得分:1)
System.out.printf("Formated double: '%s'", String.format (formatString, inputDouble));