JAVA数字处理类使用2
生活随笔
收集整理的這篇文章主要介紹了
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));}}轉載于:https://www.cnblogs.com/java0721/archive/2011/12/29/2602522.html
總結
以上是生活随笔為你收集整理的JAVA数字处理类使用2的全部內容,希望文章能夠幫你解決所遇到的問題。