[Java]常用數字函數

字串取小數幾位,不足碼補零

package com.yslifes.util;

import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.text.DecimalFormatSymbols;

public class NumberUtils {
    /**
     * 字串取小數幾位
     * 
     * @param str
     *            字串(數字)
     * @param precision
     *            小數幾碼
     * @return 取好的字串
     */
    public static String MarkUpZero(String str, int precision) {
        String maskup = "00000000000";
        String retVal = str;

        retVal = StringUtils.nvl(retVal, "0");

        if (precision == 0) {
            int idx = retVal.indexOf(".");
            if (idx == -1)
                return retVal;
            else
                return retVal.substring(0, idx);
        }
        if (precision > 0 && retVal.indexOf(".") == -1) {
            retVal += ".";
        }
        int len = retVal.indexOf(".");

        return (retVal + maskup).substring(0, retVal.indexOf("."))
                + (retVal + maskup).substring(retVal.indexOf("."), len
                        + precision + 1);

    }
    public static void main(String args[]) {
        System.out.println("22222取小數二位:\t\t\t"+MarkUpZero("22222", 2));
        System.out.println("22222.1取小數二位:\t\t"+MarkUpZero("22222.1", 2));
        System.out.println("22222.11111取小數二位:\t\t"+MarkUpZero("22222.11111", 2));
        System.out.println("22222.1取小數零位:\t\t"+MarkUpZero("22222.1", 0));
        
        
    }
}

Read More

[Java]常用的字串String函數function

字串或數字不足碼部份補零

/**
     * 數字不足部份補零回傳
     * @param str 數字
     * @param lenSize 數字最大長度,不足的部份補零
     * @return 回傳補零後字串數字
     */
    public static String MakesUpZero(int str, int lenSize) {
        return MakesUpZero(String.valueOf(str), lenSize);
    }

    /**
     * 數字不足部份補零回傳
     * @param str 字串
     * @param lenSize 字串數字最大長度,不足的部份補零
     * @return 回傳補零後字串數字
     */
    public static String MakesUpZero(String str, int lenSize) {
        String zero = "0000000000";
        String returnValue = zero;

        returnValue = zero + str;

        return returnValue.substring(returnValue.length() - lenSize);

    }
    public static void main(String args[])
    {
        //把字串123前方不足五碼的部份補零
        System.out.println("不足5碼部份補零:"+StringUtils.MakesUpZero("123", 5));
        //把數字123前方不足六碼的部份補零
        System.out.println("不足6碼部份補零:"+StringUtils.MakesUpZero(123, 6));
    }

Read More

Log4net Visual Studio版的log4j

在撰寫Java程式時很習慣的使用log4j來當程式訊息的輸出記錄,只需要簡單的改變設定檔就可以改變訊息是要顯示於Console、存入資料庫或是存於檔案裡,而且可選擇對於不同層級的記錄,十分方便於開發的Debug。

log4j也有.net版本,就叫log4net,也是由Apache基金會開發維護,在使用上與log4j還蠻相似的,log4j在使用時只需把config檔放置於classes跟目錄就可以了,而log4net則需要手動載入此設定檔,安裝設定的方法如下。

Read More

All in One SEO中文描述Descriptions支援問題解決方法

wordpress的外掛All in one SEO不知道在幾版後突然就對繁體中文的支援度降低了(消失?)了,在options裡設定自動取得meta descriptions對於中文文章並不會有作用,而且英文字詞間是以空白做為分格與中文字連續沒有分格不同,像是”Hello World !”與”哈囉世界!”,在取descriptions就會有明顯有問題。

搜尋了一下資料找到了一個解決方式,如下:

1.先到外掛編輯器

1.jpg

2.選擇要編輯的外掛All in One SEO Pack,然後按下選取,選擇All-in-one-seo-pack/aioseop.class.php這個檔案。

Read More