java printf 格式带有字符串参数的 double

标签 java format printf

在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/

相关文章:

JavaFX ListView 不支持 String.format?

php - PHP中变量扩展与sprintf的性能

java - 如何在不每次加载 URL 的情况下从 JPanel 调整子 WebView/JFXPanel 的大小?

java - 字符串分割特殊正则表达式

model - 从头开始生成3D模型的文件格式?

python - python 中是否有类似 date_diff 的东西来获取明天的日期?

java - Spring 依赖注入(inject) : How to Instantiate Class based on system property?

javascript - 错误 :android-apt plugin is incompatible with the Android Gradle plugin. 请改用 'annotationProcessor' 配置,问题?

c - ntohl 在 sin_port 上使用并得到负数

c - 为什么 strcpy() 也在复制\n?我可以摆脱它吗?