• 1.3输出任意实数


    public class test {
    
        public static void printDigit(int n){
            System.out.print(n);
        }
        
        public static void printInt(int n){
            if(n>=10){
                printInt(n/10);
            }
            printDigit(n%10);
        }
        public static int getInt(double n){
            return (int)n;
        }
        public static double getFraction(double n){
            return (n-(int)n);
        }
        public static void printFraction(double fraction,int dec){
            int i,tmp;
            for(i=0;i<dec;i++){
                fraction = fraction *10;
                tmp = getInt(fraction);
                printDigit(tmp);
                fraction = getFraction(fraction);
            }
        }
        public static double runUp(double n,int dec){
            int i;
            double tmp = 0.5;
            for(i =0;i<dec;i++){
                tmp = tmp / 10;
            }
            return (n + tmp);
        }
        public static void printReal(double n,int dec){
            int tmp1;
            double tmp2;
            if(n<0)
                n = -n;
            n = runUp(n,dec);
            tmp1 = getInt(n);
            tmp2 = getFraction(n);
            printInt(tmp1);
            if(dec > 0){
                System.out.print(".");
                printFraction(tmp2,dec);
            }
        }
        public static void main(String[] args) {
            // TODO Auto-generated method stub
            printReal(4.65,2);
        }
    }

    注意:runUp函数,否则和输出的不一定对。

  • 相关阅读:
    2016年3月3日
    性能测试之我解
    Vim命令合集
    vi-vim常用命令
    架构的本质是
    网站三层架构学习之一 分层式结构
    Spring 4 + Quartz 2.2.1 Scheduler Integration Example
    “城市民族工作条例”详解--建议废止
    字符串匹配处理
    LogBack简易教程
  • 原文地址:https://www.cnblogs.com/aitixiaocai/p/4374351.html
Copyright © 2020-2023  润新知