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

  • 相关阅读:
    魅族note手机 图片打马赛克
    魅蓝note手机一键root
    eclipse启动时return code 13
    Eclipse adt 23中运行百度地图官网上的demo出现fatal错误
    windows上部署svn服务器
    windows下jenkins slave 搭建
    图片懒加载
    tomcat7 设置静态资源的expires过期时间
    如何将不带www的根域名301重定向到带www的主域名
    简单的防止图片另存为
  • 原文地址:https://www.cnblogs.com/Thui/p/15722481.html
Copyright © 2011-2022 走看看