千分位以下四舍五入逻辑的实现(Rounding Numbers)

算是搞清楚了四舍五入的算法。

 

代码如下:

package example;
//JHTP Exercise 6.10: Rounding Numbers
//by pandenghuang@163.com
/**(Rounding Numbers) To round numbers to specific decimal places, use a statement like
y = Math.floor(x * 10 + 0.5) / 10;
which rounds x to the tenths position (i.e., the first position to the right of the decimal point), or
y = Math.floor(x * 100 + 0.5) / 100;
which rounds x to the hundredths position (i.e., the second position to the right of the decimal
point). Write an application that defines four methods for rounding a number x in various ways:
a) roundToInteger(number)
b) roundToTenths(number)
c) roundToHundredths(number)
d) roundToThousandths(number)
For each value read, your program should display the original value, the number rounded to the
nearest integer, the number rounded to the nearest tenth, the number rounded to the nearest hundredth
and the number rounded to the nearest thousandth.*/
import java.util.Scanner;

public class RoundingNumbers 
{
	public static double RoundNumber(double number,int precision){
		double rounded=0.0;
		if (precision==0)
			rounded = Math.floor(number+0.5);
		else
			rounded = Math.floor(number * Math.pow(10, precision-1)*10 + 0.5) / (10*Math.pow(10, precision-1));
		return rounded;
	}
public static void main(String[] args)
{
	double number=0.0;
	int precision=1;
	double rounded=0.0;
	Scanner input=new Scanner(System.in);
	
	
	do {
		System.out.print("请输入要四舍五入的数字(输入-1退出):");
		number=input.nextDouble();
		if(number==-1)
			System.out.print("已退出程序");
		else
		{
		System.out.print("请输入要四舍五入的精度(整数请输入0,十分位请输入1,百分位2,依次类推):");
		precision=input.nextInt();
		rounded=RoundNumber (number,precision);
		switch (precision){
		case 3:
			System.out.printf("原数为:%f,千分位四舍五入后为:%.3f\n",number,rounded);
		case 2:
			System.out.printf("原数为:%f,百分位四舍五入后为:%.2f\n",number,rounded);
		case 1:
			System.out.printf("原数为:%f,十分位四舍五入后为:%.1f\n",number,rounded);
		case 0:
			System.out.printf("原数为:%f,四舍五入取整后为:%.0f\n",number,rounded);
		}
		}
		}
	while (number!=-1);
} 
} 

 

运行结果:

请输入要四舍五入的数字(输入-1退出):3.115
请输入要四舍五入的精度(整数,十分位请输入1,百分位2,依次类推): 2
原数为:3.115000,百分位四舍五入后为:3.12
原数为:3.115000,十分位四舍五入后为:3.1
原数为:3.115000,四舍五入取整后为:3
请输入要四舍五入的数字(输入-1退出): 3.5
请输入要四舍五入的精度(整数,十分位请输入1,百分位2,依次类推):2
原数为:3.500000,百分位四舍五入后为:3.50
原数为:3.500000,十分位四舍五入后为:3.5
原数为:3.500000,四舍五入取整后为:4
请输入要四舍五入的数字(输入-1退出):4.5589
请输入要四舍五入的精度(整数,十分位请输入1,百分位2,依次类推):3
原数为:4.558900,千分位四舍五入后为:4.559
原数为:4.558900,百分位四舍五入后为:4.56
原数为:4.558900,十分位四舍五入后为:4.6
原数为:4.558900,四舍五入取整后为:5
请输入要四舍五入的数字(输入-1退出):4.3484
请输入要四舍五入的精度(整数,十分位请输入1,百分位2,依次类推):3
原数为:4.348400,千分位四舍五入后为:4.348
原数为:4.348400,百分位四舍五入后为:4.35
原数为:4.348400,十分位四舍五入后为:4.3
原数为:4.348400,四舍五入取整后为:4
请输入要四舍五入的数字(输入-1退出):-1
已退出程序

 



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值