zoukankan      html  css  js  c++  java
  • java indexof、BigDecimal、字符串替换

    自我总结,有什么需要改正弥补的地方,请指出,感激不尽!

    本次总结了indexof的用法BigDecimal的乘法、移位运算Decimal的格式化输出,字符串替换

    上代码:

    测试类 Test.java

    package com.core;
    
    import java.io.IOException;
    import java.math.BigDecimal;
    import java.text.DecimalFormat;
    import java.util.regex.Matcher;
    import java.util.regex.Pattern;
    
    public class Test {
        public static void main(String[] args) throws IOException {
            //indexof返回在当前字符串里要查询的字符的下标位置,若不存在返回-1
            String cname = "尼古拉斯凯奇";
            System.out.println(cname.indexOf("斯"));
            String ename = "Nicolas Cage";
            System.out.println(ename.indexOf(" "));
            System.out.println(ename.indexOf("b"));
    
            //BigDecimal一般用在有金钱交易的平台中,float和double不能保证较高的数值精确度
            BigDecimal b = new BigDecimal("0.41");
            long m = b.multiply(new BigDecimal("300")).longValue(); // 0.41*300
            b = new BigDecimal("0.09");
            long s = b.movePointRight(2).longValue(); // 0.09小数点右移两位
            String result = Long.valueOf(m - s).toString();
            System.out.println(result);
            
            //DecimalFormat格式化输出
            DecimalFormat df = new DecimalFormat("#0.00");
            DecimalFormat df1 = new DecimalFormat("#.00");
            Double percent = 0.965;
            System.out.println(df.format(percent));
            System.out.println(df1.format(percent));
            
            //替换字符串里的字符
            String name = "敏感词汇";
            Pattern p = Pattern.compile("敏感");
            Matcher m1 = p.matcher(name);
            if (m1.find()) {
                System.out.println(m1.replaceAll("和谐"));
            }
        }
    }

    打印结果:

    3
    7
    -1
    114
    0.96
    .96
    和谐词汇
  • 相关阅读:
    Apache的Order Allow,Deny 详解
    apache的AllowOverride以及Options使用详解
    安装启动apache2.4后报Invalid command 'order', perhaps misspelled or defined by a module not included
    前端常见跨域解决方案(全)
    php面试宝典
    php面试题2018
    nginx负载均衡的5种策略
    多台服务器共享session问题
    小程序定义并使用类
    微信小程序真机预览接口不到数据,打开调试确能请求到
  • 原文地址:https://www.cnblogs.com/xxyfhjl/p/3975307.html
Copyright © 2011-2022 走看看