• java 浮点数


    package precisenumber;

    //import java.util.*;
    public class PreciseNumber {
    public int fore;
    public int after;

    public void DTI(double num)// double to int
    {
    String numStr = Double.toString(num);
    String foreStr = numStr.substring(0, numStr.indexOf("."));
    String afterStr = numStr.substring(numStr.indexOf(".") + 1);
    this.fore = Integer.parseInt(foreStr);
    this.after = Integer.parseInt(afterStr);
    }

    public double ITD()// int to double
    {
    String numStr = this.fore + "." + this.after;
    double num = Double.parseDouble(numStr);
    return num;
    }

    public void putNum(double num) {
    System.out.println("number" + num);
    }

    public static void main(String[] args) {

    }
    }

    package precisenumber;

    import java.util.Scanner;

    public class text {
    private PreciseNumber Num1 = new PreciseNumber();
    private PreciseNumber Num2 = new PreciseNumber();
    private PreciseNumber result = new PreciseNumber();

    public double getNum() {
    Scanner in = new Scanner(System.in);
    double num = in.nextDouble();
    return num;
    }

    public void add(double num1, double num2) {
    Num1.DTI(num1);
    Num2.DTI(num2);
    result.fore = Num1.fore + Num2.fore;
    result.after = Num1.after + Num2.after;
    double num;
    num = result.ITD();
    result.putNum(num);
    }

    public void subtract(double num1, double num2) {
    Num1.DTI(num1);
    Num2.DTI(num2);
    result.fore = Num1.fore - Num2.fore;
    result.after = Num1.after - Num2.after;
    double num;
    num = result.ITD();
    result.putNum(num);
    }

    public void multiplication(double num1, double num2) {
    Num1.DTI(num1);
    Num2.DTI(num2);
    result.fore = Num1.fore * Num2.fore;
    result.after = Num1.after * Num2.after;
    double num;
    num = result.ITD();
    result.putNum(num);
    }

    public void devide(double num1, double num2) {
    Num1.DTI(num1);
    Num2.DTI(num2);
    result.fore = Num1.fore / Num2.fore;
    result.after = Num1.after / Num2.after;
    double num;
    num = result.ITD();
    result.putNum(num);
    }

    public static void main(String[] args) {
    text operate = new text();
    System.out.println("Input num 1:");
    double num1 = operate.getNum();
    System.out.println("Input num 2:");
    double num2 = operate.getNum();
    System.out.println("选择操作:");
    System.out.println("1.加 2.减 3.乘 4.除");
    Scanner in = new Scanner(System.in);
    int i = in.nextInt();
    switch (i) {
    case 1:
    operate.add(num1, num2);
    break;
    case 2:
    operate.subtract(num1, num2);
    break;
    case 3:
    operate.multiplication(num1, num2);
    break;
    case 4:
    operate.devide(num1, num2);
    break;
    }
    }

    }

  • 相关阅读:
    linux jdk1.8安装
    spring boot jar包 linux 部署
    mysql linux安装
    codesmith 三层架构
    数据库分库分表(sharding)系列(三) 关于使用框架还是自主开发以及sharding实现层面的考量
    android学习路线
    博客资源
    dotNET跨平台研究的相关文档
    Foundation框架—— 数组 (NSArray NSMutableArray )
    OC面向对象特性: 继承
  • 原文地址:https://www.cnblogs.com/the-wang/p/6441853.html
Copyright © 2020-2023  润新知