关于几个 数值型 操作的问题 1. 如何生成指定范围的随机数 , 例如  生成 从 100 到 300 , 例如 : 生成 从 0.001到 3.5
2.字符型如何转浮点型3.如何将一个字符转成ascII码4. X的y次方,例如2的7次方5. 如何表达 16 进制6. 如何平方根  
7. 如何求余数 ,  例如 3 / 2 返回 18. 小数位不四舍五入 , 例如 : 21.875 保留 2位小数 返回 21.87 9. 小数位并四舍五入,  例如 : 21.875 保留 2位小数 返回 21.88 ,例如 : 21.875 保留 1位小数 返回 21.9

解决方案 »

  1.   

    1. Random ran = new Random();
      // 100~300 包含 100,但不包含 300
      int k = ran.nextInt(300 - 100) + 100;
      // ran * (max - min) + min
      double d = ran.nextDouble() * (3.5 - 0.001) + 0.001;2. String str = "1.5";
      double d = Double.parseDouble(str);
      float f = Float.parseFloat(str);3. char c = 'a';
      int ascii = (int)c;4. double d = Math.pow(2, 7);5. int num = 29;
      String hex = Integer.toString(num, 16);6. double d = Math.sqrt(2);7. int remainder = 3 % 2;8. public static void main(String[] args) {
        double d = 31.777;
        System.out.println(round(d, 2));
        System.out.println(floor(d, 2));
      }  public static double floor(double d, int dec) {
        int p = (int) Math.pow(10, dec);
        double round = (int) Math.floor(d * p) / (p + 0.0);
        return round;
      }9. public static double round(double d, int dec) {
        int p = (int) Math.pow(10, dec);
        double round = (int) Math.round(d * p) / (p + 0.0);
        return round;
      }楼主啊,没事要多看看 API DOC 哦~~
      

  2.   

    bao110908(火龙果)
    你好厉害哈
      

  3.   

    1>   int a[] = new  (int)(Math.random()).nextInt(100,300)
     2>    String j = "12344"   float f = Float.ParseFloat(j)
      
    3>   char a = 'ab'  ; int c = (int)a  
     4> Math.pow(x,y);
    5> 0xxx  后面的是16进制的数字
    6> int n = sqrt(m); 
    7> int n = x/y     
    8> 四舍五入的话,有round()函数可以搞定,要显示位数的就要用格式化问题
     Math.round(x,y)  y是指定保留的数字,或者指定输出格式化`