如何在java中将整数转换为bigdecimal
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/49853552/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how to convert integer to bigdecimal in java
提问by ema
I want to create a method that caculates multiplication an integer and a bigdecimal. I search on google and forums nothing I found.
我想创建一个计算乘法整数和大十进制的方法。我在谷歌和论坛上搜索我没有找到任何东西。
import java.math.BigDecimal;
private Integer quantite;
private BigDecimal prixUnit;
public Integer getQuantite() {
return quantite;
}
public void setQuantite(Integer quantite) {
this.quantite = quantite;
}
public BigDecimal getPrixUnit() {
return prixUnit;
}
public void setPrixUnit(BigDecimal prixUnit) {
this.prixUnit = prixUnit;
}
public BigDecimal methCal(BigDecimal quantite, BigDecimal prixUnit){
this.prixUnit=prixUnit;
BigDecimal j = new BigDecimal(quantite);
this.j = quantite;
return quantite*prixUnit ;
}
Can someone help me?
有人能帮我吗?
采纳答案by Sheepy
To multiply an integer (or byte/short/float/double) with a BigInteger (or BigDecimal), you must convert the native number to BigInteger/BigDecimal first.
要将整数(或字节/短整数/浮点数/双精度数)与 BigInteger(或 BigDecimal)相乘,您必须先将本机数转换为 BigInteger/BigDecimal。
// int parameter can be int or Integer
public static BigInteger multiply ( int a, BigInteger b ) {
return BigInteger.valueOf( a ).multiply( b );
}
// BigInteger <> BigDecimal
public static BigDecimal multiply ( int a, BigDecimal b ) {
return BigDecimal.valueOf( a ).multiply( b );
}
// same for add, subtract, divide, mod etc.
Note:
valueOf
is not the same asnew
, and for different reasons on BigDecimaland BigInteger. In both cases, I recommendvalueOf
overnew
.
注意:
valueOf
是不一样的new
,而且在不同的原因的BigDecimal和BigInteger的。在这两种情况下,我都推荐valueOf
超过new
.
I see that you added your code, nice.
It doesn't work because Integer is mixed with BigDecimal, and also *
does not work with BigDecimal.
If you compare it with my code, the fix should be obvious:
我看到你添加了你的代码,很好。它不起作用,因为 Integer 与 BigDecimal 混合,并且*
也不适用于 BigDecimal。如果将其与我的代码进行比较,则修复应该很明显:
public BigDecimal methCal ( int quantite, BigDecimal prixUnit ) {
return BigDecimal.valueOf( quantite ).multiply( prixUnit );
}
回答by Jeroen Steenbeeke
Google definitely could have helped you, if you know what to look for:
如果您知道要查找的内容,Google 绝对可以帮助您:
https://docs.oracle.com/javase/9/docs/api/java/math/BigDecimal.html#BigDecimal-int-
https://docs.oracle.com/javase/9/docs/api/java/math/BigDecimal.html#BigDecimal-int-
This is one of the constructors for BigDecimal, which allows you to do the following:
这是 BigDecimal 的构造函数之一,它允许您执行以下操作:
BigDecimal five = BigDecimal.valueOf(5);
BigDecimal seven = BigDecimal.valueOf(2).add(five);
Seeing as you stated you wanted to multiply an int and a BigDecimal, this would be achieved as follows:
正如您所说,您想将一个 int 和一个 BigDecimal 相乘,这将按如下方式实现:
BigDecimal result = yourBigDecimal.multiply(BigDecimal.valueOf(yourInt));
And, supposing you want this result as an int:
并且,假设您希望此结果为 int:
int intResult = result.intValue();
Keep in mind that this throws away the fraction though. If you want rounding instead:
请记住,这会丢弃分数。如果你想要四舍五入:
int intResult = result.round(0, RoundingMode.HALF_UP).intValue();
回答by Imal
These methods from the Java API will be helpful.
Java API 中的这些方法会很有帮助。
public BigDecimal multiply?(BigDecimal multiplicand)
Returns a BigDecimal whose value is (this × multiplicand), and whose scale is (this.scale() + multiplicand.scale()).
返回一个 BigDecimal,其值为 (this × multiplicand),其比例为 (this.scale() + multiplicand.scale())。
Parameters: multiplicand - value to be multiplied by this BigDecimal. Returns: this * multiplicand
参数: multiplicand - 要与此 BigDecimal 相乘的值。返回:这个 * 被乘数
public BigDecimal?(int val)
Translates an int into a BigDecimal. The scale of the BigDecimal is zero.
将 int 转换为 BigDecimal。BigDecimal 的标度为零。
Parameters: val - int value to be converted to BigDecimal.
参数: val - 要转换为 BigDecimal 的 int 值。
回答by Minoli Alles
Try this:
尝试这个:
import java.math.*;
public class calculator{
public static void main(String[] args) {
BigDecimal value1 = new BigDecimal("3383878445");
BigDecimal returnValue = calculation(2,value1);
System.out.println("value is :" + returnValue);
}
public static BigDecimal calculation(int no1, BigDecimal no2){
BigDecimal value = BigDecimal.valueOf(no1).multiply(no2);
return value;
}
}