zoukankan      html  css  js  c++  java
  • 06_Math 的 floor,round 和 ceil 方法实例比较_格式化字符串_String类

    创建格式化字符串

    我们知道输出格式化数字可以使用 printf() 和 format() 方法。

    String 类使用静态方法 format() 返回一个String 对象而不是 PrintStream 对象。

    String 类的静态方法 format() 能用来创建可复用的格式化字符串,而不仅仅是用于一次打印输出。

    如下所示:

    System.out.printf("浮点型变量的值为 " +
                      "%f, 整型变量的值为 " +
                      " %d, 字符串变量的值为 " +
                      "is %s", floatVar, intVar, stringVar);
    
    
    String fs;
    fs = String.format("浮点型变量的值为 " +
                       "%f, 整型变量的值为 " +
                       " %d, 字符串变量的值为 " +
                       " %s", floatVar, intVar, stringVar);

    Math 的 floor,round 和 ceil 方法实例比较

    public class Main {   
      public static void main(String[] args) {   
        double[] nums = { 1.4, 1.5, 1.6, -1.4, -1.5, -1.6 };   
        for (double num : nums) {   
          test(num);   
        }   
      }   
      
      private static void test(double num) {   
        System.out.println("Math.floor(" + num + ")=" + Math.floor(num));   
        System.out.println("Math.round(" + num + ")=" + Math.round(num));   
        System.out.println("Math.ceil(" + num + ")=" + Math.ceil(num));   
      }   
    }
    
    
    Math.floor(1.4)=1.0
    Math.round(1.4)=1
    Math.ceil(1.4)=2.0
    Math.floor(1.5)=1.0
    Math.round(1.5)=2
    Math.ceil(1.5)=2.0
    Math.floor(1.6)=1.0
    Math.round(1.6)=2
    Math.ceil(1.6)=2.0
    Math.floor(-1.4)=-2.0
    Math.round(-1.4)=-1
    Math.ceil(-1.4)=-1.0
    Math.floor(-1.5)=-2.0
    Math.round(-1.5)=-1
    Math.ceil(-1.5)=-1.0
    Math.floor(-1.6)=-2.0
    Math.round(-1.6)=-2
    Math.ceil(-1.6)=-1.0

     

     https://www.runoob.com/java/java-string.html

  • 相关阅读:
    CodeForces
    EOJ 3506. 斐波那契数列
    牛客练习赛13 D幸运数字Ⅳ . 康托逆展开
    UVA
    Piggy-Bank HDU
    Dollar Dayz POJ
    UVA 674 Coin Change (完全背包)
    python OOP (1)
    python lambda简易使用
    python whl模块安装方法
  • 原文地址:https://www.cnblogs.com/Thui/p/15722481.html
Copyright © 2011-2022 走看看