gpt4 book ai didi

Java BigDecimal 类

转载 作者:行者123 更新时间:2023-12-01 05:29:28 25 4
gpt4 key购买 nike

这是一项家庭作业,我正在尝试编写一个利息计算器。我需要使用 BigDecimal 类,但无法弄清楚如何以货币或百分比形式输出。我不太确定要问什么问题,但我将发布 BigDecimal 类代码和另一个以我需要的方式显示输出但不使用 BigDecimal 的代码。任何建议表示赞赏。

   import java.util.Scanner;

import java.math.*;

public class project3a
{


public static void main(String[] args)
{
System.out.println("Welcome to the interest calculator");
System.out.println();


// create a scanner object and start while loop
Scanner sc = new Scanner(System.in);
String choice ="y";
while (choice.equalsIgnoreCase("y"))
{

//Get input from user
System.out.print("Enter Loan amount: ");
double Loanamount = sc.nextDouble();

System.out.print("Enter Interest Rate: ");
double interestrate = sc.nextDouble();

//calculate results
double Interest = Loanamount * interestrate;

//format
BigDecimal decimalLoanamount = new BigDecimal(Double.toString (Loanamount));
decimalLoanamount = decimalLoanamount.setScale(2, RoundingMode.HALF_UP);
BigDecimal decimalinterestrate = new BigDecimal(Double.toString(interestrate));
BigDecimal decimalInterest = new BigDecimal (Double.toString(Interest));
decimalInterest = decimalInterest.setScale(2,RoundingMode.HALF_UP);

//Display results
System.out.println("Loan amount: " + decimalLoanamount);
System.out.println("Interest rate: " + decimalinterestrate);
System.out.println("Interest:" + decimalInterest);
System.out.println();

//see if user wants to continue
System.out.print("Continue? (y/n): ");
choice = sc.next();
System.out.println();


}

}

}
<小时/>
    import java.util.Scanner;
import java.text.NumberFormat;
import java.math.*;

public class project3a
{


public static void main(String[] args)
{
System.out.println("Welcome to the interest calculator");
System.out.println();


// create a scanner object and start while loop
Scanner sc = new Scanner(System.in);
String choice ="y";
while (choice.equalsIgnoreCase("y"))
{

//Get input from user
System.out.print("Enter Loan amount: ");
double Loanamount = sc.nextDouble();

System.out.print("Enter Interest Rate: ");
double interestrate = sc.nextDouble();

//calculate results
double Interest = Loanamount * interestrate;

//format and display results
NumberFormat currency = NumberFormat.getCurrencyInstance();
NumberFormat percent = NumberFormat.getPercentInstance();
String message =
"Loan amount: " + currency.format (Loanamount) + "\n"
+ "Interest rate: " + percent.format (interestrate)+ "\n"
+ "Interest: " + currency.format (Interest) + "\n";

System.out.println(message);

//see if user wants to continue
System.out.print("Continue? (y/n): ");
choice = sc.next();
System.out.println();


}

}

}

最佳答案

BigDecimal 具有可在 NumberFormat 中使用的方法 doubleValue()

BigDecimal bd = new BigDecimal("15.25");
NumberFormat currency = NumberFormat.getCurrencyInstance();
NumberFormat percent = NumberFormat.getPercentInstance();

System.out.println( currency.format( bd.doubleValue() ) ); //In Brazil outputs R$ 15,25
System.out.println( percent.format( bd.doubleValue() ) ); // 1.525%

关于Java BigDecimal 类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9221996/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com