3

Say I want to return something like this: "Title bookTitle, Author bookAuthor: $cost"

The ones in bold are variables. I can't figure out how to return this using string.format("%s .., blah blah")

3 Answers 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);
Sign up to request clarification or add additional context in comments.

Comments

5

2 strings and a float with precision 2

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

Comments

2
String fs;
fs = String.format("Title %t, Author %a: $%c", bookTitle, bookAuthor, cost);
System.out.println(fs);

http://download.oracle.com/javase/tutorial/java/data/strings.html

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.