"Thinkinginjava" 26, Java string format specifier

Source: Internet
Author: User
Tags string format

/*** Book: Thinking in Java * Features: Formatting parameters for the Java string format specifier * format string syntax as follows: *%[argument_index$][flags][width][.precision] conversion* file: receipt.java* time: April 11, 2015 19:40:53* Author: cutter_point*/package Lesson13_strings;import Java.util.formatter;public class Receipt {private Double total = 0;private Formatter f = new Formatter (system.out);//Specifies the output Destination public void PrintTitle ()//Output Caption {//This format the first%-15s is said to be a width of 15 string, followed by similar, just-still not very clear what is a F.format ("%-15s%5s%10s\n", "Item", " Qty "," price "), F.format ("%-15s%5s%10s\n ","----","---","-----");} public void print (String name, Int. qty, double price) {F.format ("%-15.15s%5d%10.2f\n", Name, qty, price); public void Printtotal () {F.format ("%-15.15s%5s%10.2f\n", "tax", "", total*0.06); F.format ("%-15s%5s%10s\n", "", "", "-- ----"); F.format ("%-15.15s%5s%10.2f\n "," Total "," ", total*1.06);} public static void Main (String [] args) {Receipt Receipt = new Receipt (); Receipt.printtitle (); Receipt.print ("Jack ' s Magic    Beans ", 4, 4.25); Receipt.print ("PrincessPeas ", 3, 5.1);    Receipt.print ("Three Bears porridge", 1, 14.29);    Receipt.printtotal ();    Formatter ff = new Formatter (System.out); Ff.format ("%5d", 998);}}


Output:

item              Qty       price
----             ---      -----
Jack ' s Magic be     4        4.25
Princess peas       3       5.10
Three Bears por     1      14.29
tax                           1.42
                          ------
total                       25.06
  998





"Thinkinginjava" 26, Java string format specifier

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.