日韩黑丝制服一区视频播放|日韩欧美人妻丝袜视频在线观看|九九影院一级蜜桃|亚洲中文在线导航|青草草视频在线观看|婷婷五月色伊人网站|日本一区二区在线|国产AV一二三四区毛片|正在播放久草视频|亚洲色图精品一区

分享

[Jakarta Commons筆記] org.apache.commons.lang.math

 moonboat 2008-11-12

Jakarta Commons中,專門處理數(shù)學(xué)計算的類分別可以在兩個地方找到:一是Commons Langorg.apache.commons.lang.math包中,二是在Commons Math這個單獨(dú)的子項目中。由于后者主要是處理復(fù)數(shù)、矩陣等,相對使用比較少,在我的筆記中就只簡單講講Commons Lang中的math包。對后者感興趣的可以看看

http://jakarta./commons/math/

 

org.apache.commons.lang.math包中共有10個類,這些類可以歸納成四組:

1- 處理分?jǐn)?shù)的Fraction類;

2- 處理數(shù)值的NumberUtils類;

3- 處理數(shù)值范圍的RangeNumberRange、IntRangeLongRange、FloatRangeDoubleRange類;

4- 處理隨機(jī)數(shù)的JVMRandomRandomUtils類。

 

下面我舉個例子分別說明上述四組的使用方法:

 

package sean.study.jakarta.commons.lang;

 

import org.apache.commons.lang.ArrayUtils;

import org.apache.commons.lang.BooleanUtils;

import org.apache.commons.lang.StringUtils;

import org.apache.commons.lang.math.DoubleRange;

import org.apache.commons.lang.math.Fraction;

import org.apache.commons.lang.math.NumberUtils;

import org.apache.commons.lang.math.RandomUtils;

import org.apache.commons.lang.math.Range;

 

public class LangMathUsage {

 

    public static void main(String[] args) {

        demoFraction();

        demoNumberUtils();

        demoNumberRange();

        demoRandomUtils();

    }

 

    public static void demoFraction() {

        System.out.println(StringUtils.center(" demoFraction ", 30, "="));

        Fraction myFraction = Fraction.getFraction(144, 90);

        // Fraction myFraction = Fraction.getFraction("1 54/90");

        System.out.println("144/90 as fraction: " + myFraction);

        System.out.println("144/90 to proper: " + myFraction.toProperString());

        System.out.println("144/90 as double: " + myFraction.doubleValue());

        System.out.println("144/90 reduced: " + myFraction.reduce());

        System.out.println("144/90 reduced proper: "

                + myFraction.reduce().toProperString());

        System.out.println();

    }

 

    public static void demoNumberUtils() {

        System.out.println(StringUtils.center(" demoNumberUtils ", 30, "="));

        System.out.println("Is 0x3F a number? "

                + StringUtils.capitalize(BooleanUtils.toStringYesNo(NumberUtils

                        .isNumber("0x3F"))) + ".");

        double[] array = { 1.0, 3.4, 0.8, 7.1, 4.6 };

        double max = NumberUtils.max(array);

        double min = NumberUtils.min(array);

        String arrayStr = ArrayUtils.toString(array);

        System.out.println("Max of " + arrayStr + " is: " + max);

        System.out.println("Min of " + arrayStr + " is: " + min);

        System.out.println();

    }

 

    public static void demoNumberRange() {

        System.out.println(StringUtils.center(" demoNumberRange ", 30, "="));

        Range normalScoreRange = new DoubleRange(90, 120);

        double score1 = 102.5;

        double score2 = 79.9;

        System.out.println("Normal score range is: " + normalScoreRange);

        System.out.println("Is "

                + score1

                + " a normal score? "

                + StringUtils

                        .capitalize(BooleanUtils.toStringYesNo(normalScoreRange

                                .containsDouble(score1))) + ".");

        System.out.println("Is "

                + score2

                + " a normal score? "

                + StringUtils

                        .capitalize(BooleanUtils.toStringYesNo(normalScoreRange

                                .containsDouble(score2))) + ".");

        System.out.println();

    }

 

    public static void demoRandomUtils() {

        System.out.println(StringUtils.center(" demoRandomUtils ", 30, "="));

        for (int i = 0; i < 5; i++) {

            System.out.println(RandomUtils.nextInt(100));

        }

        System.out.println();

    }

 

}

 

以下是運(yùn)行結(jié)果:

 

======== demoFraction ========

144/90 as fraction: 144/90

144/90 to proper: 1 54/90

144/90 as double: 1.6

144/90 reduced: 8/5

144/90 reduced proper: 1 3/5

 

====== demoNumberUtils =======

Is 0x3F a number? Yes.

Max of {1.0,3.4,0.8,7.1,4.6} is: 7.1

Min of {1.0,3.4,0.8,7.1,4.6} is: 0.8

 

====== demoNumberRange =======

Normal score range is: Range[90.0,120.0]

Is 102.5 a normal score? Yes.

Is 79.9 a normal score? No.

 

====== demoRandomUtils =======

75

63

40

21

92

 

以上就是Commons Langmath包通常的用法。提一下NumberUtils.inNumber(String)方法,它會正確判斷出給定的字符串是否是合法的數(shù)值,而我在前面的筆記中提到的StringUtils.isNumeric(String)只能判斷一個字符串是否是由純數(shù)字字符組成。Commons Langmath包的各個類都還有很多實用的方法,遠(yuǎn)不止我在例子中用到的這些,如果你感興趣,參照一下Commons LangJavadoc吧。

    本站是提供個人知識管理的網(wǎng)絡(luò)存儲空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點。請注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購買等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請點擊一鍵舉報。
    轉(zhuǎn)藏 分享 獻(xiàn)花(0

    0條評論

    發(fā)表

    請遵守用戶 評論公約

    類似文章 更多