- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
此代码在我提到的 setScale(); 行给我一个错误它给我一个错误,说需要四舍五入。我正在尝试制作一个应用程序,它将接受 cp 和 sp,然后根据利润/损失找到利润/损失百分比。有人可以帮我处理 BigDecimal 语句吗?
```java
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.Scanner;
abstract class functions
{
abstract void cpSP();
}
public class Percentage
{
static Scanner sc = new Scanner(System.in);
public void cpSP()
{
System.out.println("Enter the Cost Price:");
double CP = sc.nextDouble();
System.out.println("Enter the Selling Price:");
double SP = sc.nextDouble();
if (CP > SP)
{
System.out.println("As the Cost Price is greater than the Selling Price, there will be LOSS.");
System.out.println("Loss = CP - SP");
double loss = CP - SP;
System.out.println("Loss = " + loss);
System.out.println("Now,");
System.out.println("Loss percentage = Loss/CP x 100");
System.out.println("");
double lossPercentage = (loss/CP)*100;
String lPString = Double.toString(lossPercentage);
BigDecimal bd = new BigDecimal(lPString);
bd.setScale(2);
System.out.println("Loss percentage = " + bd + "%" );
}
else if (SP > CP)
{
System.out.println("As the Cost Price is less than the Selling Price, there will be PROFIT.");
System.out.println("Profit = SP - CP");
double profit = SP - CP;
System.out.println("Profit = " + profit);
System.out.println("Now,");
System.out.println("Profit percentage = Profit/CP x 100");
double profitPercentage = (profit / CP)*100;
String pPString = Double.toString(profitPercentage);
BigDecimal bd = new BigDecimal(pPString).setScale(2, RoundingMode.UNNECESSARY);
System.out.println("Proft percentage = " + bd + "%" );
}
else
{
System.out.println("No profit, no loss.");
}
}
public static void main(String[] args)
{
Percentage p = new Percentage();
System.out.println("Select an option:");
System.out.println("1. Find profit/loss percentage where the CP and SP is given.");
int select = sc.nextInt();
if (select == 1)
{
p.cpSP();
}
}
}
```
错误是:
Exception in thread "main" java.lang.ArithmeticException: Rounding necessary
at java.math.BigDecimal.commonNeedIncrement(BigDecimal.java:4148)
at java.math.BigDecimal.needIncrement(BigDecimal.java:4204)
at java.math.BigDecimal.divideAndRound(BigDecimal.java:4112)
at java.math.BigDecimal.setScale(BigDecimal.java:2452)
at java.math.BigDecimal.setScale(BigDecimal.java:2386)
at maths.Percentage.cpSP(Percentage.java:45)
at maths.Percentage.main(Percentage.java:64)
最佳答案
在setScale
方法的javadoc中明确提到
@throws ArithmeticException if {@code roundingMode==ROUND_UNNECESSARY} and the specified scaling operation would require rounding.
例子:这将抛出上述异常
new BigDecimal("1200.1234").setScale(2, BigDecimal.ROUND_UNNECESSARY)
因为当您将它的比例减小到 2 时,您需要指定如何对其进行舍入 - 向上舍入 (1200.13) 或向下舍入 (1200.12)
如果您只需要两位小数,请选择一种舍入方式(以您可接受的为准)
关于java - BigDecimal setScale 方法语法不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51172726/
这个问题已经有答案了: Why do immutable classes provide mutators? (1 个回答) 已关闭 8 年前。 感谢您的建议。 这是为什么: import java.
在我当前使用SpriteKit的项目中,我有一堆需要在不同时间分别放大和缩小的Sprite。问题在于,当我缩放节点时,物理主体不会随之缩放,因此会破坏物理状态。这是我为解决这个问题而编写的一个小示例:
有人请告诉我我犯的错误: 我的号码:225.0453,内部保存有 4 位数字。这是一个美元货币数字,我想要 pretty-print 来显示。我用RoundingMode.HALF_EVEN 由于美元
我正在这样做: (obj.getValue().divide(weight)).setScale(3, RoundingMode.HALF_DOWN) 我收到此错误: java.lang.Arithm
此代码在我提到的 setScale(); 行给我一个错误它给我一个错误,说需要四舍五入。我正在尝试制作一个应用程序,它将接受 cp 和 sp,然后根据利润/损失找到利润/损失百分比。有人可以帮我处理
setValue(BigDecimal value) { this.value=value; this.value.setScale(8, RoundingMode.H
考虑这段代码: import java.math.BigDecimal; import java.math.RoundingMode; public class RoundingTests {
任何人都可以帮助我使用 BigDecimal,我是新手 我想将 24.33 舍入到 25.00 我只需要执行天花板操作 所以我做了这样的事情 BigDecimal amount = new BigDe
你好,我正在尝试使用 libGDX 制作一个简单的游戏,但我在使用 sprite 类时遇到了一些问题,这是我的代码: package com.carebearer; import com.badlog
我正在使用 libgdx,但使用 BitmapFont 类给了我一个非常巨大的文本。即使我将比例设置为非常小的值(0.1f),字体仍然会以巨大的尺寸呈现。 Libgdx 文档没有解释我应该使用的正常值
我正在关注这个 YouTube 系列节目,您可以在其中学习如何在出现错误时编写游戏程序,因此我修复了该错误,但这导致了另一个错误。而且我很确定我所做的与 YouTuber 完全一样。 所以,如果您知道
我们有以下代码: BigDecimal net = price .divide(taxCumulative, RoundingMode.HALF_UP) .setScale(2, BigDecimal
我目前正在尝试缩放字体,但收到错误消息“方法 setScale(float, float) 未定义 BitmapFont 类型”这是我收到错误的代码部分,特别是在第 2 行和第 4 行。 fo
如果我使用 KineticJS 设置比例/缩放,我会收到一个奇怪的错误。出现随机线条。 EDIT1:好的。看起来如果我将 scaleRate 设置为 0.25、0.125、0.0625 等,它就可以工
public class Dummy { public static void main(String args[]) { String x = "1.234.567,89 E
我正在尝试使用以下命令指定BigDecimal值的精度:new BigDecimal(12.99).setScale(2, BigDecimal.ROUND_HALF_EVEN。但是,编译器告诉我se
我正在尝试在用户获得额外生命时缩放 Sprite 并在 1 或 2 秒后将其设置回正常值。 我有这个: if (_score == 2) { _life = _life + 1; st
在我的 Android 应用程序中,我加载了一张图片。用户可以使用这张图片放大、缩小和前后移动它。目前我一次只能让一个人工作。 经过大量测试后,我确定我所说的第二个是有效的。 matrix.setSc
我将以下代码与 half_even 舍入模式的 java BigDecimal setScale 方法一起使用,并得到以下结果。 new BigDecimal(1.115).setScale(2, R
为了对数字进行四舍五入,我使用以下代码: public static roundBd(BigDecimal bd){ BigDecimal result1 = bd.setScale(0, Rou
我是一名优秀的程序员,十分优秀!