标签:四舍五入 sig boolean ignore cal tin bool 常量 方法
package com.importc;
public class stringDemo01 {
public static void main(String[] args) {
String s1 = "Hello";
String s2 = "HELLO";
//比较两个字符串是否相同
System.out.println(s1.equals(s2)); //false
//忽略大小写比较
System.out.println(s1.equalsIgnoreCase(s2)); //true
//是否包含字符串
System.out.println(s1.contains("ll")); //true
//开头,结尾,下标
System.out.println(s1.indexOf("He")); //0
System.out.println(s1.lastIndexOf("l")); //3
System.out.println(s1.startsWith("He")); // true
System.out.println(s1.endsWith("lo")); //true
//提取字符串
System.out.println(s1.substring(2)); //llo
System.out.println(s1.substring(2, 4)); //ll 闭开
//去除字符串空白
System.out.println(" Hello\n".trim());
//判断是空还是空白字符串
System.out.println(" ".isEmpty()); //false
System.out.println("".isEmpty()); //true
//替换字符串
System.out.println(s1.replace("l", "~")); //He~~o
//分割字符串
String[] ss = "A,B,C,D".split(",");
for (String s: ss) {
System.out.print(s+" "); //A B C D
}
//拼接字符串 视情况使用StringJoiner
String arr[] = {"A", "B", "C"};
String ass2 = String.join("*", arr);
System.out.println(ass2); //A*B*C
}
}
package com.importc;
public class StringBuilderDemo {
public static void main(String[] args) {
StringBuilder sb = new StringBuilder(1024);
for (int i = 0; i < 100; i++) {
sb.append(i);
sb.append("-");
}
String s = sb.toString();
System.out.println(s.substring(0, sb.length()-1));
}
}
StringBuilder还可以进行链式操作,如下:
package com.importc;
public class StringBuilderDemo {
public static void main(String[] args) {
StringBuilder sb = new StringBuilder(1024);
sb.append("Good").append(" Morning").append(" Bob").insert(0, "Oh ");
System.out.println(sb.toString()); // Oh Good Morning Bob
}
}
可以进行链式操作的关键在于,StringBuilder中方法结束都会返回this当前实例,所以可以接着调用自身的方法。
我们可以仿照这个用法,链式执行一个人一天的作息,如下:
ThisPerson
package com.importc;
public class ThisPerson {
public ThisPerson eat() {
System.out.print("吃饭-->");
return this;
}
public ThisPerson sleep() {
System.out.print("睡觉-->");
return this;
}
public ThisPerson study() {
System.out.print("学习");
return this;
}
}
Application
package com.importc;
public class Application {
public static void main(String[] args) {
ThisPerson tp = new ThisPerson();
tp.eat().sleep().study(); // 吃饭-->睡觉-->学习
}
}
package com.importc;
import com.oop.demo05.B;
import java.math.BigDecimal;
import java.math.RoundingMode;
public class BigDecimalDemo {
public static void main(String[] args) {
BigDecimal bd = new BigDecimal("1234.5678");
System.out.println(bd.multiply(bd)); //1524157.65279684
BigDecimal d1 = new BigDecimal("123.45");
BigDecimal d2 = new BigDecimal("123.4500");
BigDecimal d3 = new BigDecimal("1234500");
System.out.println(d1.scale()); //输出小数位数 2
System.out.println(d2.scale()); //4
System.out.println(d3.scale()); //0
BigDecimal d4 = d3.stripTrailingZeros();
System.out.println(d4.scale()); //-2
//BigDecima在做加减乘时精度不会丢失,但是在做除法时,可能存在除不尽的情况
BigDecimal d5 = bd.divide(d1, 10, RoundingMode.HALF_UP); //保留十位小数并四舍五入
//BigDecimal d6 = bd.divide(d1); //会报错ArithmeticException
System.out.println(d5);
//比较两个BigDecimal
System.out.println(d1.equals(d2)); //false 大小相同但是小数位数不同
System.out.println(d1.compareTo(d2)); //0表示等于 负数表示小于 正数表示大于
}
}
package com.importc;
import java.util.Random;
public class MathDemo {
public static void main(String[] args) {
//绝对值
System.out.println(Math.abs(-6)); //6
//最大值 最小值
System.out.println(Math.max(1, 2)); //2
System.out.println(Math.min(3, 4)); //3
//计算x的y次方
System.out.println(Math.pow(2, 3)); //8.0
//数学常量 PI E
System.out.println(Math.PI);
System.out.println(Math.E);
//生成随机数(伪)
System.out.println(Math.random()); //[0,1)
Random r = new Random();
System.out.println(r.nextInt());
System.out.println(r.nextBoolean());
System.out.println(r.nextFloat());
System.out.println(r.nextDouble());
}
}
标签:四舍五入 sig boolean ignore cal tin bool 常量 方法
原文地址:https://www.cnblogs.com/lucaz/p/14412691.html