本文记录一些开发过程中遇到的知识点,便于查询。细节决定成败,会持续更新~
double计算精度问题(使用BigDecimal)
- 上代码
// 参数double d1 = 4.015; double d2 = 1000;
private static void testDoubleMultiply(double d1, double d2) {
double result1= d1*d2;
System.out.println(" d1*d2 ="+result1);
//这里输出结果:d1*d2 =4014.9999999999995
double result2= BigDecimal.valueOf(d1).multiply(BigDecimal.valueOf(d2)).doubleValue();
System.out.println(" d1 multiply d2 ="+result2);
//这里输出结果:d1 multiply d2 =4015.0
}
提示:使用BigDecimal进行精确运算(+-*/),用法自行google吧。
正则匹配,替换字符串中指定字符
Pattern p = Pattern.compile("cat");
Matcher m = p.matcher("one cat two cats in the yard");
StringBuffer sb = new StringBuffer();
while (m.find()) {
m.appendReplacement(sb, "dog");
}
m.appendTail(sb);
System.out.println(sb.toString());
//output: one dog two dogs in the yard
split分隔字符串
//测试参数
String str = "1230145201257012557401577055987";
String str1 = "01230145201257012557401577055987";
String str2= "12301452012570125574015770559870";
String[] split = originStr.split("0");
System.out.println(Arrays.toString(split)+" size = "+split.length);
//output: [123, 1452, 1257, 125574, 1577, 55987] size = 6
// [, 123, 1452, 1257, 125574, 1577, 55987] size = 7
// [123, 1452, 1257, 125574, 1577, 55987] size = 6
如果分隔符regularExpression(这里测试使用的“0”)在开始位置,则分割后数组第一个元素为”“