In Java, I want to scan user for formatString and inputDouble as follows:
Scanner scan = new Scanner(System.in);
System.out.println("Enter formating string and double to format:");
String formatString = scan.next();
Double inputDouble = scan.nextDouble();
But for now, let's say I only have
String formatString = "%.2f";
Double inputDouble = 42.424242;
I would to use the System.out.printf(); to format the inputDouble based on the formatString. Something like:
System.out.printf("Formated double: '%s'", formatString, inputDouble);
so something in form:
System.out.printf("Formated double: 'magicGoesHere'", formatString, inputDouble);
So in this particular case, I would like to see output:
Formated double: 42.42
Is it possible somehow? thanks
Split the call up into parts:
System.out.print("Formated double: '");
System.out.printf(formatString, inputDouble);
System.out.print("'");
System.out.printf("Formated double: '%s'", String.format (formatString, inputDouble));
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.