JAVA数字处理类使用2

尝试开发一个程序: 定义一个求圆面积的方法,其中以圆半径作为参数,将计算结果保留5位小数。

import java.text.DecimalFormat; /** * 尝试开发一个程序: * 定义一个求圆面积的方法,其中以圆半径作为参数,将计算结果保留5位小数。 * @author HAN * */ /*public class ch9_2 { public BigDecimal surface_circle(double radius){ double a=2*Math.PI*Math.pow(radius, 2); BigDecimal b=new BigDecimal(Double.toString(a)); BigDecimal b2=new BigDecimal(Double.toString(1)); return b.divide(b2,5,BigDecimal.ROUND_HALF_UP); } public ch9_2(){ } public static void main(String[] args) { ch9_2 obj= new ch9_2(); System.out.println(obj.surface_circle(1)); } } */ public class DataTreatementClassApps2 { public double surface_circle(double radius){ double a=2*Math.PI*Math.pow(radius, 2); //运用了pow()方法和PI常量 DecimalFormat myformat=new DecimalFormat(".#####");//DecimalFormat格式化数字类 String out=myformat.format(a); String newout=out.replace(',', '.'); //小数的变换 Double d=new Double(newout); //System.out.println(myformat.format(a)); return d.doubleValue(); } public DataTreatementClassApps2(){ } public static void main(String[] args) { DataTreatementClassApps2 obj= new DataTreatementClassApps2(); System.out.println(obj.surface_circle(50.8)); } }

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值