码迷,mamicode.com
首页 > 编程语言 > 详细

java 浮点数

时间:2017-02-25 16:00:55      阅读:193      评论:0      收藏:0      [点我收藏+]

标签:precise   ble   sys   sub   stat   操作   opera   public   rest   

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 浮点数

标签:precise   ble   sys   sub   stat   操作   opera   public   rest   

原文地址:http://www.cnblogs.com/the-wang/p/6441853.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!