如何去掉BigDecimal得科學計數法和保留7位小數問題關
阿新 • • 發佈:2019-02-02
<span style="background-color: rgb(255, 255, 255);"><span style="font-size:18px;"><span style="text-indent: 2em;">給定圓的半徑r,求圓的面積。</span>要求四捨五入保留小數點後7位。PI=atan(1.0)*4。</span></span>
以下是第一種java程式碼:
第二種方法:import java.math.BigDecimal; import java.util.Scanner; public class Main { public static void main(String[] args) { double PI = 3.14159265358979323; Scanner scanner = new Scanner(System.in); int r = scanner.nextInt(); double d = 0; d = PI * r * r; BigDecimal decimal = new BigDecimal(d); BigDecimal b = decimal.setScale(7,BigDecimal.ROUND_HALF_UP); System.out.println(b.toPlainString()); scanner.close(); } }
import java.util.Scanner;
public class Main
{
public static void main(String[] args)
{
double PI = Math.atan(1.0)*4;
Scanner scanner = new Scanner(System.in);
int r = scanner.nextInt();
String s = String.format("%.7f", PI * r * r);
System.out.println(s);
scanner.close();
}
}
通過以上兩種方法可以使浮點數四捨五入並取7位小數