Read Sean

          Read me, read Sean.
          posts - 508, comments - 655, trackbacks - 9, articles - 4

          導航

          公告


          • 關于我
          • 人生是一場對話
          • gaoyuxiang AT gmail DOT com
          • titlelogo.png

          • 我的譯作
          • Scala for the Impatient
          • Open Source SOA
          • Programming in Scala

          • 聲明
          • 所有文章和代碼在這里以"現狀"提供,作者不提供任何形式的擔保,也沒有授予除閱讀和有條件共享之外的任何權利。除非特別說明,所有文章均為本blog作者原創,如需轉載請注明出處和原作者,如用于商業目的,需作者本人書面許可。

          • 推薦文章
          • Pylons起步

          常用鏈接

          留言簿(29)

          隨筆分類(842)

          隨筆檔案(507)

          文章檔案(4)

          Friends' blogs

          搜索

          •  

          積分與排名

          • 積分 - 782845
          • 排名 - 54

          最新評論

          閱讀排行榜

          評論排行榜

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

          Posted on 2005-08-01 13:18 laogao 閱讀(3984) 評論(3)  編輯  收藏 所屬分類: On Java

           

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

          http://jakarta.apache.org/commons/math/

           

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

          1- 處理分數的Fraction類;

          2- 處理數值的NumberUtils類;

          3- 處理數值范圍的RangeNumberRangeIntRangeLongRangeFloatRangeDoubleRange類;

          4- 處理隨機數的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();

              }

           

          }

           

          以下是運行結果:

           

          ======== 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)方法,它會正確判斷出給定的字符串是否是合法的數值,而我在前面的筆記中提到的StringUtils.isNumeric(String)只能判斷一個字符串是否是由純數字字符組成。Commons Langmath包的各個類都還有很多實用的方法,遠不止我在例子中用到的這些,如果你感興趣,參照一下Commons LangJavadoc吧。

           

           

          Feedback

          # re: [Jakarta Commons筆記] org.apache.commons.lang.math  回復  更多評論   

          2006-01-15 10:37 by 大嘴巴
          google summer code 上的project,作者是叫zhangxiaogang,是您嗎?

          # re: [Jakarta Commons筆記] org.apache.commons.lang.math  回復  更多評論   

          2006-01-15 21:21 by 大胃
          不是。

          # re: [Jakarta 筆記] org.apache.commons.lang.math[未登錄]  回復  更多評論   

          2012-12-17 14:37 by doctor
          Commons math 或JmathLib,在eclipse中怎么使用,我想寫個將掃描的網格線數據轉換成函數式的工具,現存的不好用,如:getData,FindGraph,engauge,QQ;873128294,zmhhxl@yahoo.com.cn
          主站蜘蛛池模板: 南溪县| 航空| 卢龙县| 专栏| 延吉市| 镇赉县| 正镶白旗| 晋城| 香格里拉县| 高雄市| 太仓市| 措勤县| 盈江县| 怀化市| 湘潭市| 孟津县| 仙桃市| 霍林郭勒市| 祁门县| 九龙城区| 合水县| 汝城县| 乐安县| 乌恰县| 长泰县| 甘德县| 双牌县| 贺兰县| 广丰县| 和田县| 中阳县| 东山县| 息烽县| 五常市| 英山县| 开远市| 奇台县| 靖远县| 都兰县| 南木林县| 延长县|