%ld 在 Java 中的 C++ 等价于 String.format()

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2011720/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 02:30:31  来源:igfitidea点击:

C++ Equivalent of %ld in Java for String.format()

javastring.format

提问by Milli

For instance, %11.2lfin C++ becomes %11.2fin Java. How about for long format?

例如,%11.2lf在 C++ 中变成%11.2f在 Java 中。长格式怎么样?

采纳答案by Andrzej Doyle

As you may have worked out, it's not necessary to specify the lflag. According to the docs, a decimal integer is specified by djust like in C++. So the answer is just %d.

正如您可能已经发现的那样,没有必要指定l标志。根据docs,十进制整数由d就像在 C++ 中指定。所以答案是%d

回答by Stu Thompson

Use %dfor decimals (long, int). It works OK. E.g.:

使用%d的小数(长,INT)。它工作正常。例如:

System.err.println(String.format("%d", 193874120937489387L));

...will print just fine. Read up on java.util.Formatterfor more details. %dwill take a long, no problem.

...会打印得很好。请继续阅读以java.util.Formatter了解更多详情。 %d会用long,没问题。