欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

java 中 String format 和Math类实例详解

程序员文章站 2023-12-13 20:24:04
java 中 string format 和math类实例详解 java字符串格式化输出 @test public void test() {...

java 中 string format 和math类实例详解

java字符串格式化输出

@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); 
} 

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 
  } 

随机数

//随机数 
  @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); 
  } 
@test 
public void test5(){ 
  random r = new random(); 
  int a = r.nextint(101);//0-100 
   
} 

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将日期对象生成指定格式的字符串 
       
       
    } 

感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!

上一篇:

下一篇: