Java 小知识

本文记录一些开发过程中遇到的知识点,便于查询。细节决定成败,会持续更新~

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”)在开始位置,则分割后数组第一个元素为”“

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值