成功捷徑,貴在堅持
          人為善,福雖未至禍已遠離; 人為惡,禍雖未至福已遠離
          文章出自http://www.cujava.com
          import java.math.BigDecimal;
          import java.text.DecimalFormat;

          public class TestGetInt{
          ? public static void main(String[] args){
          ? ? double i=2, j=2.1, k=2.5, m=2.9;
          ? ? System.out.println("舍掉小數取整:Math.floor(2)=" + (int)Math.floor(i));
          ? ? System.out.println("舍掉小數取整:Math.floor(2.1)=" + (int)Math.floor(j));
          ? ? System.out.println("舍掉小數取整:Math.floor(2.5)=" + (int)Math.floor(k));
          ? ? System.out.println("舍掉小數取整:Math.floor(2.9)=" + (int)Math.floor(m));
          ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?
          ? ? /* 這段被注釋的代碼不能正確的實現四舍五入取整
          ? ? System.out.println("四舍五入取整:Math.rint(2)=" + (int)Math.rint(i));
          ? ? System.out.println("四舍五入取整:Math.rint(2.1)=" + (int)Math.rint(j));
          ? ? System.out.println("四舍五入取整:Math.rint(2.5)=" + (int)Math.rint(k));
          ? ? System.out.println("四舍五入取整:Math.rint(2.9)=" + (int)Math.rint(m));
          ? ?
          ? ? System.out.println("四舍五入取整:(2)=" + new DecimalFormat("0").format(i));
          ? ? System.out.println("四舍五入取整:(2.1)=" + new DecimalFormat("0").format(i));
          ? ? System.out.println("四舍五入取整:(2.5)=" + new DecimalFormat("0").format(i));
          ? ? System.out.println("四舍五入取整:(2.9)=" + new DecimalFormat("0").format(i));
          ? ? */
          ? ?
          ? ? System.out.println("四舍五入取整:(2)=" + new BigDecimal("2").setScale(0, BigDecimal.ROUND_HALF_UP));
          ? ? System.out.println("四舍五入取整:(2.1)=" + new BigDecimal("2.1").setScale(0, BigDecimal.ROUND_HALF_UP));
          ? ? System.out.println("四舍五入取整:(2.5)=" + new BigDecimal("2.5").setScale(0, BigDecimal.ROUND_HALF_UP));
          ? ? System.out.println("四舍五入取整:(2.9)=" + new BigDecimal("2.9").setScale(0, BigDecimal.ROUND_HALF_UP));

          ? ? System.out.println("湊整:Math.ceil(2)=" + (int)Math.ceil(i));
          ? ? System.out.println("湊整:Math.ceil(2.1)=" + (int)Math.ceil(j));
          ? ? System.out.println("湊整:Math.ceil(2.5)=" + (int)Math.ceil(k));
          ? ? System.out.println("湊整:Math.ceil(2.9)=" + (int)Math.ceil(m));

          ? ? System.out.println("舍掉小數取整:Math.floor(-2)=" + (int)Math.floor(-i));
          ? ? System.out.println("舍掉小數取整:Math.floor(-2.1)=" + (int)Math.floor(-j));
          ? ? System.out.println("舍掉小數取整:Math.floor(-2.5)=" + (int)Math.floor(-k));
          ? ? System.out.println("舍掉小數取整:Math.floor(-2.9)=" + (int)Math.floor(-m));
          ? ?
          ? ? System.out.println("四舍五入取整:(-2)=" + new BigDecimal("-2").setScale(0, BigDecimal.ROUND_HALF_UP));
          ? ? System.out.println("四舍五入取整:(-2.1)=" + new BigDecimal("-2.1").setScale(0, BigDecimal.ROUND_HALF_UP));
          ? ? System.out.println("四舍五入取整:(-2.5)=" + new BigDecimal("-2.5").setScale(0, BigDecimal.ROUND_HALF_UP));
          ? ? System.out.println("四舍五入取整:(-2.9)=" + new BigDecimal("-2.9").setScale(0, BigDecimal.ROUND_HALF_UP));

          ? ? System.out.println("湊整:Math.ceil(-2)=" + (int)Math.ceil(-i));
          ? ? System.out.println("湊整:Math.ceil(-2.1)=" + (int)Math.ceil(-j));
          ? ? System.out.println("湊整:Math.ceil(-2.5)=" + (int)Math.ceil(-k));
          ? ? System.out.println("湊整:Math.ceil(-2.9)=" + (int)Math.ceil(-m));
          ? ? }
          }
          posted on 2008-06-24 20:20 選寶網an9 閱讀(14944) 評論(1)  編輯  收藏 所屬分類: 轉載

          FeedBack:
          # 34800四舍五入到萬位
          2008-10-04 14:44 | 666
          34800四舍五入到萬位  回復  更多評論
            
          主站蜘蛛池模板: 锦州市| 名山县| 东方市| 巴楚县| 兴城市| 清远市| 琼结县| 台中县| 呼伦贝尔市| 治多县| 秦皇岛市| 株洲市| 永胜县| 九龙城区| 慈利县| 佛冈县| 娄底市| 若尔盖县| 尼木县| 龙州县| 黑河市| 东辽县| 牡丹江市| SHOW| 高州市| 瑞丽市| 丰顺县| 原平市| 黄陵县| 阿坝县| 如东县| 东兰县| 东丽区| 壤塘县| 石楼县| 商水县| 青海省| 和平区| 博野县| 密山市| 翁牛特旗|