最?lèi)?ài)Java

          書(shū)山有路勤為徑,學(xué)海無(wú)涯苦作舟

          org.apache.commons.lang.math學(xué)習(xí)筆記

              在org.apache.commons.lang.math包中,一共有11個(gè)類(lèi)。可以分成四組:
              1. 處理分?jǐn)?shù)的Fraction類(lèi);
              2.處理數(shù)值的NumberUtils和IEEE754rUtils類(lèi),這里IEEE745r代表的是IEEE 754的標(biāo)準(zhǔn),是一種浮點(diǎn)數(shù)的處理標(biāo)準(zhǔn)。
              3.處理隨機(jī)數(shù)的JVMRandom和RandomUtils類(lèi)。
              4.處理數(shù)值范圍的Range, DoubleRange, FloatRange, IntRange, LangRange, NumberRange類(lèi)。

              Fraction類(lèi)能很方便地處理分?jǐn)?shù),并能進(jìn)行分?jǐn)?shù)的約分,加減乘除和指數(shù)運(yùn)算以及求相對(duì)值。如:
              private void FractionDemo() {
                  Fraction fraction 
          = Fraction.getFraction(0.5);
                  System.out.println(fraction.getNumerator());
                  System.out.println(fraction.getDenominator());

                  fraction 
          = Fraction.getFraction("1/2");
                  System.out.println(fraction.doubleValue());

                  fraction 
          = Fraction.getFraction(12);
                  System.out.println(fraction.doubleValue());

                  fraction 
          = Fraction.getFraction(112);
                  System.out.println(fraction.doubleValue());

                  fraction 
          = Fraction.getFraction(24);
                  System.out.println(fraction.doubleValue());
                  fraction 
          = Fraction.getReducedFraction(24);
                  System.out.println(fraction.doubleValue());

                  System.out.println(Fraction.getFraction(
          -12).abs());
                  System.out.println(Fraction.getFraction(
          1-2).abs());
                  System.out.println(Fraction.getFraction(
          12).add(
                          Fraction.getFraction(
          12)));
                  System.out.println(Fraction.getFraction(
          12).subtract(
                          Fraction.getFraction(
          12)));
                  System.out.println(Fraction.getFraction(
          12).multiplyBy(
                          Fraction.getFraction(
          12)));
                  System.out.println(Fraction.getFraction(
          12).divideBy(
                          Fraction.getFraction(
          12)));
                  System.out.println(Fraction.getFraction(
          12).pow(2));
              }

              NumberUtils的功能相對(duì)來(lái)說(shuō)就有點(diǎn)雞肋了,他能通過(guò)createXXX(String str)創(chuàng)建各種類(lèi)型的數(shù)值,即使你傳入的參數(shù)是0X123這樣代表16進(jìn)制的數(shù),它也能正確解析出來(lái)。同時(shí)它還具有獲取數(shù)組最大最小數(shù)的功能。
              private void NumberUtilsDemo() {
                  System.out.println(
          "Is 0x3F a number? " + NumberUtils.isNumber("0x3F"));
                  
          double[] array = 1.03.40.87.14.6 };
                  
          double max = NumberUtils.max(array);
                  
          double min = NumberUtils.min(array);
                  System.out.println(
          "Max of array is: " + max);
                  System.out.println(
          "Min of array is: " + min);
                  System.out.println();
              }

                 JVMRandom繼承了java.util.Random類(lèi),其功能和Random差不多,只不過(guò)封裝了返回不同數(shù)據(jù)類(lèi)型的方法而已。而RandomUtils則把JVMRandom的方法靜態(tài)化了。
              private void RandomUtilsDemo() {
                  
          for (int i = 0; i < 5; i++{
                      System.out.println(RandomUtils.nextInt(
          100));
                  }

                  System.out.println();
              }

              Range是一個(gè)abstract類(lèi),主要處理數(shù)值范圍的。DoubleRange, FloatRange, IntRange, LangRange和NumberRange都繼承了Range類(lèi),它們差不多,只是處理的數(shù)據(jù)類(lèi)型不同而已。所以看一個(gè)類(lèi)的使用方法就等于看了所有類(lèi)的使用方法。
              private void NumberRangeDemo() {
                  Range normalScoreRange 
          = new DoubleRange(90120);
                  
          double score1 = 102.5;
                  
          double score2 = 79.9;
                  System.out.println(
          "Normal score range is: " + normalScoreRange);
                  System.out.println(
          "Is " + score1 + " a normal score? " + normalScoreRange.containsDouble(score1));
                  System.out.println(
          "Is " + score2 + " a normal score? " + normalScoreRange.containsDouble(score2));
                  System.out.println(normalScoreRange.overlapsRange(
          new IntRange(92,119)));
              }

          posted on 2009-01-08 19:43 Brian 閱讀(1293) 評(píng)論(0)  編輯  收藏 所屬分類(lèi): Jakarta Commons筆記


          只有注冊(cè)用戶(hù)登錄后才能發(fā)表評(píng)論。


          網(wǎng)站導(dǎo)航:
           

          公告


          導(dǎo)航

          <2009年1月>
          28293031123
          45678910
          11121314151617
          18192021222324
          25262728293031
          1234567

          統(tǒng)計(jì)

          常用鏈接

          留言簿(4)

          隨筆分類(lèi)

          隨筆檔案

          收藏夾

          搜索

          最新評(píng)論

          閱讀排行榜

          評(píng)論排行榜

          主站蜘蛛池模板: 长沙县| 崇礼县| 行唐县| 亳州市| 阿合奇县| 惠安县| 彰化县| 岳阳县| 昌宁县| 安丘市| 通海县| 公主岭市| 逊克县| 海林市| 南昌市| 抚松县| 南开区| 绥德县| 弋阳县| 芦山县| 池州市| 灵寿县| 商水县| 霍林郭勒市| 克什克腾旗| 阜新市| 绵竹市| 唐海县| 德安县| 隆德县| 光山县| 丹巴县| 天峻县| 怀仁县| 额济纳旗| 建平县| 新巴尔虎右旗| 沅江市| 德庆县| 潢川县| 泰州市|