Java中使用return string.format(...)格式化字符串

3

假设我想返回这样的东西: "Title 书名, 作者 作者名: $价格"

粗体部分是变量。 我无法使用string.format("%s .., blah blah")来返回这个结果。

3个回答

9
String title = "bookTitle";
String author = "bookAuthor";
double cost = 100;
String text = String.format("Title %s, Author %s: $%.2f", title, author, cost);
System.out.println(text); // prints "Title bookTitle, Author bookAuthor: $100.00"
// or System.out.pritnf("Title %s, Author %s: $%.2f", title, author, cost);

5

两个字符串和一个精度为2的浮点数

String.format("Title %s, Author %s: $%f.2", bookTitle, bookAuthor, cost);

2

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接