如何在Java中将程序的DecimalFormat设置为小数点后2位
我正在尝试制作的程序要求在程序运行后将双“收入”设置为小数点后2位。如何在Java中将程序的DecimalFormat设置为小数点后2位,java,decimal,decimalformat,Java,Decimal,Decimalformat,我正在尝试制作的程序要求在程序运行后将双“收入”设置为小数点后2位。 下面显示了我的设置方式,但在我运行程序后,它仍然会掉零或过零。有什么问题吗 println("Please enter your filing status"); println("Enter 0 for single filers,"); println(" 1 for married filing jointly,"); println(" 2 for married f
下面显示了我的设置方式,但在我运行程序后,它仍然会掉零或过零。有什么问题吗
println("Please enter your filing status");
println("Enter 0 for single filers,");
println(" 1 for married filing jointly,");
println(" 2 for married filing seperately,");
println(" 3 for head of household");
int status = readInt("Status: ");
double income = readDouble("Please enter your taxable income: ");
DecimalFormat df = new DecimalFormat("#.00");
System.out.print(df.format(income));
if ((status == 0) && (income <= SINGLE_BRACKET1))
{
println("You owe: $" + (income * .1));
}
else if ((status == 0) && (income <= SINGLE_BRACKET2))
{
println("You owe: $" + ((SINGLE_BRACKET1 * .1) +
(income - SINGLE_BRACKET1) * .15));
}
println(“请输入您的归档状态”);
println(“输入0表示单个文件管理器,”);
println(“1为夫妻共同申报,”);
println(“婚姻档案单独填写2份”);
println(“户主3”);
int status=readInt(“状态:”);
double income=readDouble(“请输入您的应税收入:”);
DecimalFormat df=新的DecimalFormat(#.00”);
系统输出打印(df格式(收入));
如果((status==0)和(&)(income使用强制格式保留小数点后两位的a):
DecimalFormat df = (DecimalFormat) DecimalFormat.getInstance();
df.setMaximumFractionalPlaces(2);
df.format(income);
或者,您可能需要查看,因为您正在格式化货币值。不确定我是否理解您的问题,但您必须格式化每个输出:
println("You owe: $" + df.format(income * .1));
或
现在,它在getInstance之后告诉不兼容类型突出显示(),只要您使用美元符号,任何有效的想法,与区域设置无关的方法就是使用NumberFormat.getCurrencyInstance()
println(String.format("You owe: $%.2f", income * .1));