亲测,数据加减,除数,日期等

package testa;

import java.math.BigDecimal;

public class test {
    /**
     * 获取数据 1位小数点
     * 
     * @param st
     * @return
     */
    public static String get2(String st) {
        try {
            int i = Integer.parseInt(st);
        } catch (Exception e) {
            if (!st.contains(".")) {
                return "0.00";
            }
        }
        // 整数
        if (!st.contains(".")) {
            return st + ".00";
        }
        if ((st.indexOf(".") + 2) == st.length()) {
            // 3.1
            return st + "0";
        }
        return st.substring(0, st.indexOf(".") + 3);

    }

    /**
     * int 或者float 相加减
     * 
     * @param d
     * @param d1
     * @return
     */
    public static int addInt(String d, String d1) {
        try {
            if ((!d.contains(".")) && (!d1.contains("."))) {
                return Integer.parseInt(d1) + Integer.parseInt(d);
            }
            int n = 0;
            if (d.contains(".")) {
                n = Integer.parseInt(""
                        + new BigDecimal(d).setScale(0,
                                BigDecimal.ROUND_HALF_UP));
            } else {
                n = Integer.parseInt(d);
            }
            if (d1.contains(".")) {
                n = n
                        + Integer.parseInt(""
                                + new BigDecimal(d1).setScale(0,
                                        BigDecimal.ROUND_HALF_UP));
            } else {
                n = n + Integer.parseInt(d1);
            }
            return n;
        } catch (Exception e) {
            return 0;
        }
    }

    public static void main(String[] args) {
        ;
        System.out.println(addInt("1.7", "") + "test.main()" + get2("3.121"));
        double i = 2, j = 2.1, k = 2.5, m = 2.9;
        System.out.println("四舍五入取整:(2)="
                + (3 + Integer.parseInt(""
                        + new BigDecimal("222.01").setScale(0,
                                BigDecimal.ROUND_HALF_UP))));
        System.out
                .println("四舍五入取整:(2.1)="
                        + new BigDecimal("-2.91").setScale(0,
                                BigDecimal.ROUND_HALF_UP));
        System.out
                .println("四舍五入取整:(2.5)="
                        + new BigDecimal("-2.01").setScale(0,
                                BigDecimal.ROUND_HALF_UP));
        System.out.println("四舍五入取整:(2.9)="
                + new BigDecimal("2.9999999999").setScale(0,
                        BigDecimal.ROUND_HALF_UP));
        String st = "6195169595684291584";
        int b = Integer.parseInt(st.substring(st.length() - 5, st.length()));
        System.out.println("test.main()" + b);
        long tmp = System.currentTimeMillis() / 1000 - (1512376913);
        System.out.println("test.main()" + tmp + "\t" + secToTime(11111));
    }

    public static String secToTime(int time) {
        String timeStr = null;
        int hour = 0;
        int minute = 0;
        int second = 0;
        if (time <= 0)
            return "00:00";
        else {
            minute = time / 60;
            if (minute < 60) {
                second = time % 60;
                timeStr = unitFormat(minute) + ":" + unitFormat(second);
            } else {
                hour = minute / 60;
                if (hour > 99)
                    return "99:59:59";
                minute = minute % 60;
                second = time - hour * 3600 - minute * 60;
                timeStr = unitFormat(hour) + ":" + unitFormat(minute) + ":"
                        + unitFormat(second);
            }
        }
        return timeStr;
    }

    public static String unitFormat(int i) {
        String retStr = null;
        if (i >= 0 && i < 10)
            retStr = "0" + Integer.toString(i);
        else
            retStr = "" + i;
        return retStr;
    }
}
 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值