首页 > 代码库 > java 浮点数

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;
}
}

}

java 浮点数