zoukankan      html  css  js  c++  java
  • 2018/12/06 L1-029 是不是太胖了 Java

    考察Java小数点的运算:

    总共有四种方案:

    1、System.out.printf() 或

    2、DecimalFormat

    3、System.out.print(String.format("%.1f", flnalNum));

    4、NumberFormat


    1、使用System.out.printf()的方案, 代码如下:

    import java.io.BufferedReader;
    import java.io.InputStreamReader;
    
    public class Main {
    
        public static void main(String[] args) throws Exception{
            BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
            double num = Integer.parseInt(br.readLine());
            double finalNum = 0;
            if(num > 100 && num <=300) {
                finalNum = (num-100)*0.9*2;
                System.out.printf("%.1f", finalNum);
            } else {
                return;
            }
    
        }
    
    }

    2、使用DecimalFormat方案:

    import java.io.BufferedReader;
    import java.io.InputStreamReader;
    import java.text.DecimalFormat;
    
    public class Main2 {
    
        public static void main(String[] args) throws Exception{
            BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
            double num = Integer.parseInt(br.readLine());
            double finalNum = 0;
            DecimalFormat df = new DecimalFormat("#.0");
            if(num > 100 && num <=300) {
                finalNum = (num-100)*0.9*2;
    //            System.out.printf("%.1f", finalNum);
                System.out.print(df.format(finalNum));
            } else {
                return;
            }
    
        }
    
    }

    3、System.out.print(String.format("%.1f", flnalNum));方案

    import java.io.BufferedReader;
    import java.io.InputStreamReader;
    
    public class Main3 {
    
        public static void main(String[] args) throws Exception{
            BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
            double num = Integer.parseInt(br.readLine());
            double finalNum = 0;
            if(num > 100 && num <=300) {
                finalNum = (num-100)*0.9*2;
    //            System.out.printf("%.1f", finalNum);
    //            System.out.print(df.format(finalNum));
                System.out.print(String.format("%.1f", finalNum));
            } else {
                return;
            }
    
        }
    
    }

    4、NumberFormat方案, 不足的是, 小数位为零的时候, 会省略小数点和后面的小数:

    import java.io.BufferedReader;
    import java.io.InputStreamReader;
    import java.text.NumberFormat;
    
    public class Main4 {
    
        public static void main(String[] args) throws Exception{
            BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
            double num = Integer.parseInt(br.readLine());
            double finalNum = 0;
            NumberFormat nf = NumberFormat.getNumberInstance();
            nf.setMaximumFractionDigits(1);
            if(num > 100 && num <=300) {
                finalNum = (num-100)*0.9*2;
                System.out.println(nf.format(finalNum));
            } else {
                return;
            }
    
        }
    
    }
  • 相关阅读:
    把信贷风险管理浓缩为50个要点(赶紧收藏吧)!
    AjaxControlToolkit没有通过WebResource.axd加载css导致ajaxToolkit:TabPanel无法显示正确的样式
    启动vmware中的虚拟机的时候,提示Failed to lock the file
    linq to xml There are multiple root elements.
    How to create a List of ValueTuple?
    SET IDENTITY_INSERT 和 DBCC CHECKIDENT
    NOT IN clause and NULL values
    ASP.NET Session and Forms Authentication and Session Fixation
    asp.net下的cookieName
    flywaydb and sql server
  • 原文地址:https://www.cnblogs.com/huangZ-H/p/10079315.html
Copyright © 2011-2022 走看看