这个程序有效,除非 nJars 的数量是 7 的倍数,否则我会得到类似 $14.999999999999998 的答案。对于 6,输出为 14.08。如何修复 7 的倍数的异常,使其显示类似 $14.99 的内容?
import java.util.Scanner;
public class Homework_17
{
private static int nJars, nCartons, totalOunces, OuncesTolbs, lbs;
public static void main(String[] args)
{
computeShippingCost();
}
public static void computeShippingCost()
{
System.out.print("Enter a number of jars: ");
Scanner kboard = new Scanner (System.in);
nJars = kboard.nextInt();
int nCartons = (nJars + 11) / 12;
int totalOunces = (nJars * 21) + (nCartons * 25);
int lbs = totalOunces / 16;
double shippingCost = ((nCartons * 1.44) + (lbs + 1) * 0.96) + 3.0;
System.out.print("$" + shippingCost);
}
}
double number = 0.9999999999999;
DecimalFormat numberFormat = new DecimalFormat("#.00");
System.out.println(numberFormat.format(number));
会给你“0.99”。您可以在右侧添加或减去 0 以获得更多或更少的小数。
或者使用右侧的“#”使附加数字可选,例如 #.## (0.30) 会删除尾随 0 变为 (0.3)。
如果您想在控制台打印/写入双精度值,请使用
System.out.printf()
或 System.out.format()
方法。
System.out.printf("\n$%10.2f",shippingCost);
System.out.printf("%n$%.2f",shippingCost);
查看 DecimalFormat:http://docs.oracle.com/javase/6/docs/api/java/text/DecimalFormat.html
你会做类似的事情:
new DecimalFormat("$#.00").format(shippingCost);
或者由于您正在使用货币,您可以了解
NumberFormat.getCurrencyInstance()
如何为您工作。
Formatter 类也是一个不错的选择。 fmt.format("%.2f", 变量); 2 这里显示你想要多少位小数。例如,您可以将其更改为 4。不要忘记关闭格式化程序。
private static int nJars, nCartons, totalOunces, OuncesTolbs, lbs;
public static void main(String[] args)
{
computeShippingCost();
}
public static void computeShippingCost()
{
System.out.print("Enter a number of jars: ");
Scanner kboard = new Scanner (System.in);
nJars = kboard.nextInt();
int nCartons = (nJars + 11) / 12;
int totalOunces = (nJars * 21) + (nCartons * 25);
int lbs = totalOunces / 16;
double shippingCost = ((nCartons * 1.44) + (lbs + 1) * 0.96) + 3.0;
Formatter fmt = new Formatter();
fmt.format("%.2f", shippingCost);
System.out.print("$" + fmt);
fmt.close();
}
使用 DecimalFormat 类来格式化双精度数
您使用 String.format() 方法。
好吧,我只是为了好玩而想到的另一种解决方案是将小数转换为字符串,然后将字符串切成 2 个字符串,一个包含点和小数,另一个包含左侧的 Int重点。之后,将小数点和小数点的字符串限制为 3 个字符,一个用于小数点,另一个用于小数点。然后重新组合即可。
double shippingCost = ((nCartons * 1.44) + (lbs + 1) * 0.96) + 3.0;
String ShippingCost = (String) shippingCost;
String decimalCost = ShippingCost.subString(indexOf('.'),ShippingCost.Length());
ShippingCost = ShippingCost.subString(0,indexOf('.'));
ShippingCost = ShippingCost + decimalCost;
那里!很简单吧?
我认为这是减少给定双精度值小数点的有效方法。
class ReduceDecimalValue {
public static void main(String[] args) {
double value = 12222.934817359387;
System.out.println(ReduceDecimalValue.decimalPoint(value, 3));
}
static double decimalPoint(double value, int count) {// value: 12222.934817359387, count: 3
// Splitting the real and decimal values.
long real = (long) value; // real: 12222
double floats = value - real; // floats: 0.934817359387
double point = Math.pow(10, count); // point: 100
// multiplying with 10 increased by count to move the decimal point to right.
// typecast by long will remove the decimal value. and converting the value to decimal points(0.934)
floats = ((long)(floats * point)) / point; // floats: ((long)(934.817359387)) / 100
// => 934 / 100 => 0.934
return real + floats; // 12222.934
}
}