Home Java javaTutorial Detailed explanation of Math and String format class instances in java

Detailed explanation of Math and String format class instances in java

Jun 30, 2017 am 10:29 AM
format java string

This article mainly introduces relevant information about the detailed explanation of String format and Math class instances in java. Friends in need can refer to

Detailed explanation of String format and Math class instances in java

javaStringFormatted output

@Test 
public void test() { 
  // TODO Auto-generated method stub 
  //可用printf(); 
  System.out.println(String.format("I am %s", "jj"));     //%s字符串 
  System.out.println(String.format("首字母是 %c", 'x'));     //%c字符 
  System.out.println(String.format("this is %b", true));   //%b布尔类型 
  System.out.println(String.format("十进制整数 %d", 34));     //%d 十进制整数 
  System.out.println(String.format("十六进制整数 %x", 34));   //%x 十六进制整数 
  System.out.println(String.format("八进制整数 %o", 34));     //%o 八进制整数 
  System.out.println(String.format("浮点 %f", 34.0));      //%f 浮点 
  System.out.println(String.format("十六进制浮点 %a", 34.0));    //%a 十六进制浮点 
  System.out.println(String.format("指数 %e", 34.0));      //%e 指数类型 
  System.out.println(String.format("通用浮点类型 %g", 34.0));    //%g 通用浮点 
  System.out.println(String.format("散列码 %h", 34));      //%h 散列码 
  System.out.println(String.format("百分比 %%"));        //%% 百分比 
  System.out.println(String.format("换行 %n"));         //%n 换行 
  System.out.println(String.format("日期与事件类型 %ty",Calendar.getInstance())); 
  System.out.println(String.format("日期与事件类型 %tm",Calendar.getInstance())); 
  System.out.println(String.format("日期与事件类型 %te",Calendar.getInstance())); 
  //%tx 日期与事件类型,x代表不同的日期与时间转换符 %ty 年 %tm月 %te 日 
  //搭配转换符的使用 
  System.out.println(String.format("%+d", 10));        //为正数或负数添加符号 
  System.out.println(String.format("|%-5d|", 10));      //%-?为左对齐 
  System.out.println(String.format("%04d", 10));       //在整数之前添加指定数量空格 
  System.out.println(String.format("%,f", 999999999.0));   //以“,”对数字分组 
  System.out.println(String.format("%(f", -999999999.0));   //使用括号包含负数 
  System.out.println(String.format("%#x", 34));        //十六进制添加0x 
  System.out.println(String.format("%#o", 34));        //八进制添加0 
  System.out.println(String.format("%#f", 34.0));       //浮点数包含小数点 
  System.out.println(String.format("%f 和%<3.1f", 34.0f));   //格式化前一个转换符所描述的参数(小数后有一位) 
  System.out.println(String.format("%3.1f", 34.0f));   // 
  System.out.println(String.format("%2$d,%1$s", "a",1));   // x$代表是第几个变量 
  //日期格式化 
  System.out.println(String.format("全部日期和时间信息%tc", new Date()));   // tc 输出全部日期和时间信息 
  System.out.println(String.format("年—月—日格式%tF", new Date()));      // tF 年—月—日格式(要大写) 
  System.out.println(String.format("月/日/年格式%tD", new Date()));      // tD 月/日/年格式(要大写) 
  System.out.println(String.format("HH:MM:SS PM/AM格式 %tr", new Date())); // tR HH:MM:SS PM/AM格式 
  System.out.println(String.format("HH:MM:SS(24小时)%tT", new Date())); // (大写)tT HH:MM:SS 24小时制 
  System.out.println(String.format("HH:MM(24小时)%tR", new Date()));  // (大写)tR HH:MM 24小时制 
   
  System.out.println(String.format(Locale.US,"英文月份简称%tb", new Date()));    // tb 输出月份简称 
  System.out.println(String.format("本地月份简称%tb", new Date()));       // tb 输出月份简称 
  System.out.println(String.format(Locale.US,"英文月份全称%tB", new Date()));    // tB 输出月份全称 
  System.out.println(String.format("本地月份全称%tB", new Date()));       // tB 输出月份全称 
  System.out.println(String.format(Locale.US,"星期简称%ta", new Date()));   // ta 输出星期简称 
  System.out.println(String.format("星期全称%tA", new Date()));        // tA 输出星期全称 
  Date date = new Date(); 
  System.out.printf("本地星期的简称:%tA%n",date); 
  //C的使用,年前两位 
  System.out.printf("年的前两位数字(不足两位前面补0):%tC%n",date); 
  //y的使用,年后两位 
  System.out.printf("年的后两位数字(不足两位前面补0):%ty%n",date); 
  //j的使用,一年的天数 
  System.out.printf("一年中的天数(即年的第几天):%tj%n",date); 
  //m的使用,月份 
  System.out.printf("两位数字的月份(不足两位前面补0):%tm%n",date); 
  //d的使用,日(二位,不够补零) 
  System.out.printf("两位数字的日(不足两位前面补0):%td%n",date); 
  //e的使用,日(一位不补零) 
  System.out.printf("月份的日(前面不补0):%te",date); 
 //H的使用 
    System.out.printf("2位数字24时制的小时(不足2位前面补0):%tH%n", date); 
    //I的使用 
    System.out.printf("2位数字12时制的小时(不足2位前面补0):%tI%n", date); 
    //k的使用 
    System.out.printf("2位数字24时制的小时(前面不补0):%tk%n", date); 
    //l的使用 
    System.out.printf("2位数字12时制的小时(前面不补0):%tl%n", date); 
    //M的使用 
    System.out.printf("2位数字的分钟(不足2位前面补0):%tM%n", date); 
    //S的使用 
    System.out.printf("2位数字的秒(不足2位前面补0):%tS%n", date); 
    //L的使用 
    System.out.printf("3位数字的毫秒(不足3位前面补0):%tL%n", date); 
    //N的使用 
    System.out.printf("9位数字的毫秒数(不足9位前面补0):%tN%n", date); 
    //p的使用 
    String str = String.format(Locale.US, "小写字母的上午或下午标记(英):%tp", date); 
    System.out.println(str);  
    System.out.printf("小写字母的上午或下午标记(中):%tp%n", date); 
    //z的使用 
    System.out.printf("相对于GMT的RFC822时区的偏移量:%tz%n", date); 
    //Z的使用 
    System.out.printf("时区缩写字符串:%tZ%n", date); 
    //s的使用 
    System.out.printf("1970-1-1 00:00:00 到现在所经过的秒数:%ts%n", date); 
    //Q的使用 
    System.out.printf("1970-1-1 00:00:00 到现在所经过的毫秒数:%tQ%n", date); 
}
Copy after login

Math

@Test 
  public void test3(){ 
    BigDecimal d = new BigDecimal("123"); 
    BigDecimal e = new BigDecimal("14455552"); 
    System.out.println(Math.pow(123, 12)); 
    System.out.println(d.pow(12)); 
     
    System.out.println(Math.ceil(12.3));//ceil,天花板 13.0 
    System.out.println(Math.floor(-12.3));//ceil,地板 -13.0 
    System.out.println(Math.round(13.3));//四舍五入  13 
    System.out.println(Math.round(-13.5));//四舍五入   -13 
    System.out.println(Math.round(-13.2));//四舍五入   -13 
    System.out.println(Math.round(-13.7));//四舍五入   -14 
  }
Copy after login

Random number

//随机数 
  @Test 
  public void test4(){ 
    System.out.println(Math.random());//返回一个随机数>=0,+b1 
    //0-100(不包括100) 
    int a =(int)(Math.random()*100); 
    //0-100(包括100) 
    int b =(int)(Math.random()*101); 
    //30-100(包括100) 
    int c =(int)(Math.random()*71+30); 
    //0-10 
    int d = (int)(Math.random()*10); 
    System.out.println(a); 
    System.out.println(b); 
    System.out.println(c); 
    System.out.println(d); 
  }
Copy after login
@Test 
public void test5(){ 
  Random r = new Random(); 
  int a = r.nextInt(101);//0-100 
   
}
Copy after login

simpledateformat

@Test 
  public void testId() throws ParseException{ 
    String s = "411123199409203013"; 
    if(s.length()==18){ 
      String b = s.substring(6, 14); 
      String sex = Integer.parseInt(s.substring(14, 17))%2==0?"女":"男"; 
      SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd"); 
      Date birth = sdf.parse(b);//将一个日期字符串按指定的格式解析成日期对象 
      sdf = new SimpleDateFormat("yyyy年MM月dd日"); 
      System.out.println("你的生日是"+sdf.format(birth)+",你的性别是"+sex); 
      String f = String.format("你的生日是%1$TY年%1$Tm月%1$Td日,你的性别是%2$s", birth,sex); 
      System.out.println(f); 
    }else if(s.length()==15){ 
      String b = s.substring(6, 14); 
      String sex = Integer.parseInt(s.substring(14, 17))%2==0?"女":"男"; 
      SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd"); 
      Date birth = sdf.parse(b);//将一个日期字符串按指定的格式解析成日期对象 
      sdf = new SimpleDateFormat("yyyy年MM月dd日"); 
      System.out.println("你的生日是"+sdf.format(birth)+",你的性别是"+sex);//format将日期对象生成指定格式的字符串 
       
       
    }
Copy after login

The above is the detailed content of Detailed explanation of Math and String format class instances in java. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Break or return from Java 8 stream forEach? Break or return from Java 8 stream forEach? Feb 07, 2025 pm 12:09 PM

Java 8 introduces the Stream API, providing a powerful and expressive way to process data collections. However, a common question when using Stream is: How to break or return from a forEach operation? Traditional loops allow for early interruption or return, but Stream's forEach method does not directly support this method. This article will explain the reasons and explore alternative methods for implementing premature termination in Stream processing systems. Further reading: Java Stream API improvements Understand Stream forEach The forEach method is a terminal operation that performs one operation on each element in the Stream. Its design intention is

PHP: A Key Language for Web Development PHP: A Key Language for Web Development Apr 13, 2025 am 12:08 AM

PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7

PHP vs. Python: Understanding the Differences PHP vs. Python: Understanding the Differences Apr 11, 2025 am 12:15 AM

PHP and Python each have their own advantages, and the choice should be based on project requirements. 1.PHP is suitable for web development, with simple syntax and high execution efficiency. 2. Python is suitable for data science and machine learning, with concise syntax and rich libraries.

PHP vs. Other Languages: A Comparison PHP vs. Other Languages: A Comparison Apr 13, 2025 am 12:19 AM

PHP is suitable for web development, especially in rapid development and processing dynamic content, but is not good at data science and enterprise-level applications. Compared with Python, PHP has more advantages in web development, but is not as good as Python in the field of data science; compared with Java, PHP performs worse in enterprise-level applications, but is more flexible in web development; compared with JavaScript, PHP is more concise in back-end development, but is not as good as JavaScript in front-end development.

PHP vs. Python: Core Features and Functionality PHP vs. Python: Core Features and Functionality Apr 13, 2025 am 12:16 AM

PHP and Python each have their own advantages and are suitable for different scenarios. 1.PHP is suitable for web development and provides built-in web servers and rich function libraries. 2. Python is suitable for data science and machine learning, with concise syntax and a powerful standard library. When choosing, it should be decided based on project requirements.

Java Program to Find the Volume of Capsule Java Program to Find the Volume of Capsule Feb 07, 2025 am 11:37 AM

Capsules are three-dimensional geometric figures, composed of a cylinder and a hemisphere at both ends. The volume of the capsule can be calculated by adding the volume of the cylinder and the volume of the hemisphere at both ends. This tutorial will discuss how to calculate the volume of a given capsule in Java using different methods. Capsule volume formula The formula for capsule volume is as follows: Capsule volume = Cylindrical volume Volume Two hemisphere volume in, r: The radius of the hemisphere. h: The height of the cylinder (excluding the hemisphere). Example 1 enter Radius = 5 units Height = 10 units Output Volume = 1570.8 cubic units explain Calculate volume using formula: Volume = π × r2 × h (4

PHP: The Foundation of Many Websites PHP: The Foundation of Many Websites Apr 13, 2025 am 12:07 AM

The reasons why PHP is the preferred technology stack for many websites include its ease of use, strong community support, and widespread use. 1) Easy to learn and use, suitable for beginners. 2) Have a huge developer community and rich resources. 3) Widely used in WordPress, Drupal and other platforms. 4) Integrate tightly with web servers to simplify development deployment.

PHP's Impact: Web Development and Beyond PHP's Impact: Web Development and Beyond Apr 18, 2025 am 12:10 AM

PHPhassignificantlyimpactedwebdevelopmentandextendsbeyondit.1)ItpowersmajorplatformslikeWordPressandexcelsindatabaseinteractions.2)PHP'sadaptabilityallowsittoscaleforlargeapplicationsusingframeworkslikeLaravel.3)Beyondweb,PHPisusedincommand-linescrip

See all articles