String.format() method of formatting strings, the meaning and usage of different placeholders

learning target:

目标如下:

  • String.format() method of formatting strings, the meaning and usage of different placeholders

Learning Content:

内容:

  1. placeholder type
    Insert image description here

  2. The String.format() method is a method of formatting strings

  3. String: A placeholder "%s" representing the string itself to be output. (Define data type as string)

  4. Integer: There is only one placeholder "%d", which represents the integer to be output. (Define data type as int)

  5. Floating point number: There is only one placeholder "%.2f", which represents the floating point number to be output, where ".2" represents the number of decimal places to be output, that is, 2 decimal places are retained. (Define the data type as double)

  6. Date: The placeholder "%tF" represents the date to be output. "%tF" means that the output date format is "year-month-day", so the format string is "%tF" and the parameter list is date. (Define the data type as Date)

  7. Time: The placeholder "%tT" represents the time to be output. "%tT" indicates that the output time format is "hour:minute:second". The parameter list is date. (Define the data type as Date)

  8. Bandwidth rate: A placeholder "%.2f" and a text "Mbps" that together represent the bandwidth rate to be output. (Define the data type as double)

  9. Currency amount: a placeholder " %.2f ", which represents the currency amount to be output, where ""Indicates the currency symbol, ".2" indicates the number of decimal places to be output, that is, retaining 2 decimal places. Therefore, the format string is "$%.2f" and the parameter list is amount. (The defined data type is double)

  10. "%e" outputs a floating point number in scientific notation

  11. Use "%o" to output an integer represented in octal notation

  12. "%x" outputs an integer represented in hexadecimal, etc.


Guess you like

Origin blog.csdn.net/YHLSunshine/article/details/132673650