zoukankan      html  css  js  c++  java
  • 数字操作类

      数字操作类是针对数字进行处理,可以使用内置数学类,完成。也可以生产随机数

    Math类

      java.lang.Math类是整个的JDK里面唯一一个与数学计算有关的程序类。着里面提供有一些基础的数学函数,这个类中的所有方法都使用了static进行定义,所有的方法都可以通过类名称直接调用,而此类中有一个round需要特别注意

        四舍五入方法定义:public static long round(double a)

    范例:观察round()方法

    1 package cn.Tony.demo;
    2 
    3 public class TestDemo {
    4     public static void main(String[] args) throws Exception {
    5         double num=151516.56515;
    6         System.out.println(Math.round(num));
    7     }
    8 }

      如果直接使用round方法处理的话默认的处理原则就是将小数位直接进位,

    范例:继续观察round的数据

     1 package cn.Tony.demo;
     2 
     3 public class TestDemo {
     4     public static void main(String[] args) throws Exception {
     5         System.out.println(Math.round(15.51));//16
     6         System.out.println(Math.round(15.5));//16
     7         //如果负数小数没大于0.5都不进位
     8         System.out.println(Math.round(-15.5));//-15
     9         System.out.println(Math.round(-15.51));//-16
    10     }
    11 }

      我希望可以准确的保存小数位处理

     1 package cn.Tony.demo;
     2 
     3 class MyMath{
     4     /**
     5      * 进行数据的四舍五入操作
     6      * @param num    表示原始操作数据
     7      * @param scale    表示保留的小数位数
     8      * @return    已经正确四舍五入后的内容
     9      */
    10     public static double round(double num,int scale) {
    11         return Math.round(num*Math.pow(10, scale))/Math.pow(10, scale);
    12     }
    13 }
    14 public class TestDemo {
    15     public static void main(String[] args) throws Exception {
    16          System.out.println(MyMath.round(1823.2567,2));
    17     }
    18 }

      这种四舍五入的操作是最简单的处理模式。以后的开发一定会是使用到

    随机类:Random

      在很多语言里面都支持随机数子的产生,那么在Java里面使用java.util.Random类来实现这种随机数的处理;

    范例:产生随机数

     1 package cn.Tony.demo;
     2 
     3 import java.util.Random;
     4 
     5 public class TestDemo {
     6     public static void main(String[] args) throws Exception {
     7         Random rand=new Random();
     8         for(int x=0;x<10;x++) {    //100表示最大值,数据的范围是0到99
     9             System.out.println(rand.nextInt(100)+".");
    10         }
    11     }
    12 }

      在许多的网站上的英文随机验证码实际上就可以通过此类模式完成,

     1 package cn.Tony.demo;
     2 
     3 import java.util.Random;
     4 public class TestDemo {
     5     public static void main(String[] args) throws Exception {
     6         char data[]=new char[] {'a','b','c','d'};
     7         Random rand=new Random();
     8         for(int x=0;x<3;x++) {    
     9             System.out.println(data[rand.nextInt(data.length)]+".");
    10         }
    11     }
    12 }

      如果是中文验证码也很简单,写一个中文验证码库。

    大数字的操作类:

      一般的开发中使用的数据都包含在给定的数据之中,但是有时候数据真的很大,超过了预期的范围。原始程序中可以使用字符串解决。如果要进行数学计算,则取出每一位进行手工的处理。但是这种操作很麻烦。所以java.math包里面提供有两个类:BigInteger BigDecimal。这两个类都属于Number类,

    范例:观察BigInteger操作

     1 package cn.Tony.demo;
     2 import java.math.BigInteger;
     3 public class TestDemo {
     4     public static void main(String[] args) throws Exception {
     5         BigInteger bigA=new BigInteger("46515615615151111111");
     6         BigInteger bigB=new BigInteger("1161561");
     7         System.out.println("加法计算:"+bigA.add(bigB));
     8         System.out.println("减法计算:"+bigA.subtract(bigB));
     9         System.out.println("乘法计算:"+bigA.multiply(bigB));
    10         System.out.println("除法计算:"+bigA.divide(bigB));
    11         BigInteger result[]=bigA.divideAndRemainder(bigB);
    12         System.out.println("商"+result[0]+",余数"+result[1]);
    13     }
    14 }

    范例:大小数BigDecimal

     1 package cn.Tony.demo;
     2 import java.math.BigDecimal;
     3 class MyMath{
     4     public static double round(double num,int scale) {
     5         return new BigDecimal(num).divide(new BigDecimal(1),scale,BigDecimal.ROUND_HALF_UP).doubleValue();
     6     }
     7 }
     8 public class TestDemo {
     9     public static void main(String[] args) throws Exception {
    10         System.out.println(MyMath.round(16516.561, 2));
    11     }
    12 }

      关于四舍五入的处理实际上有两类做法。

      这些给定的默认的数学操作的功能都十分有限,在开法里面如果你真的用到复杂的逻辑,你要找其他的开发包去做。

  • 相关阅读:
    CMake 用法导览
    Irrlicht 1.8.4 + Win7 + VC2015 + x64 +OpenGL编译
    VirtualBox 5.1.14 获取VirtualBox COM对象错误
    CGAL Manual/tutorial_hello_world.html
    CGAL 介绍
    Open CASCADE 基础类(Foundation Classes)
    OpenCASCADE 基础
    Nginx 反向代理详解
    修改docker容器中的hosts文件
    Jmeter 设置连接oracle数据库
  • 原文地址:https://www.cnblogs.com/Tony98/p/10497024.html
Copyright © 2011-2022 走看看