gpt4 book ai didi

java - 计算 MPG 结果出现 java.lang.ArithmeticException : Non terminating decimal expansion; no exact representable decimal result

转载 作者:行者123 更新时间:2023-11-30 05:51:27 25 4
gpt4 key购买 nike

代码:

import java.util.Scanner;
import java.math.BigDecimal;
import java.math.RoundingMode;

public class MPGAppBigDecimal
{
public static void main(String[] args)
{
System.out.println("Welcome to the Miles Per Gallon Calculator.");
System.out.println();

Scanner sc = new Scanner(System.in);
String choice = "y";

while (choice.equalsIgnoreCase("y"))
{
// get the miles driven from the user
System.out.print("Enter miles driven: ");
String milesString = sc.next();

// get the gallons of gas used
System.out.print("Enter gallons of gas used: ");
String gallonsString = sc.next();

// calculating miles per gallons
BigDecimal miles = new BigDecimal(milesString);
BigDecimal gallons = new BigDecimal(gallonsString);

BigDecimal mpg = miles.divide(gallons).setScale(2, RoundingMode.HALF_UP);

// display the result
System.out.println("Miles per gallon is " + mpg.toString() + ".");
System.out.println();

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

当我输入十进制值时,它抛出异常:线程“main”中的异常 java.lang.ArithmeticException:非终止十进制扩展;没有精确可表示的十进制结果。

最佳答案

来自BigDecimal的java文档:

When a MathContext object is supplied with a precision setting of 0 (for example, MathContext.UNLIMITED), arithmetic operations are exact, as are the arithmetic methods which take no MathContext object. (This is the only behavior that was supported in releases prior to 5.) As a corollary of computing the exact result, the rounding mode setting of a MathContext object with a precision setting of 0 is not used and thus irrelevant. In the case of divide, the exact quotient could have an infinitely long decimal expansion; for example, 1 divided by 3. If the quotient has a nonterminating decimal expansion and the operation is specified to return an exact result, an ArithmeticException is thrown. Otherwise, the exact result of the division is returned, as done for other operations.

在代码中:

miles.divide(gallons)

您将英里除以加仑,但没有定义比例并检索此错误,因为您正在使用方法 public BigDecimal divide(BigDecimal divisor)这使用了无限的精度。

Returns a BigDecimal whose value is (this / divisor), and whose preferred scale is (this.scale() - divisor.scale()); if the exact quotient cannot be represented (because it has a non-terminating decimal expansion) an ArithmeticException is thrown.

使用divide(BigDecimal divisor, int scale, RoundingMode roundingMode)相反:

Returns a BigDecimal whose value is (this / divisor), and whose scale is as specified. If rounding must be performed to generate a result with the specified scale, the specified rounding mode is applied.

如下:

miles.divide(gallons, 2, RoundingMode.HALF_UP);  

关于java - 计算 MPG 结果出现 java.lang.ArithmeticException : Non terminating decimal expansion; no exact representable decimal result,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53836351/

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