如何在Java中存储长小数。

huangapple go评论160阅读模式
英文:

How to store long decimal numbers in Java

问题

以下是您提供的内容的翻译部分:

我有一段用 Java 写的代码,内容如下:

btmpW / imgW

但在这种情况下,我得到的值是 0。我尝试使用 intlongfloatdouble 作为数据类型,但仍然返回 0

当我尝试对两个 int 值使用 Log.i() 时,我得到了这个输出:

I/btmpW: 548
I/imgW: 1041

所以我使用计算器进行计算,得到了 0.5346341463…… 我对 Java 还不熟悉,但我认为 Java 不接受超过 9 位数字的数值,这让我认为 Java 只返回 (int)0,由于数字超过 9 位,它只忽略了小数部分。所以我搜索到类似 BigDecimalBigInteger 的东西。所以我尝试使用它们,但是我得到了一些错误。

所以我想知道,在 Java 中是否有一种方法可以存储这种值。btmpWimgW 是用户定义的,所以我不知道当我运行 btmpW / imgW 时会得到什么结果,基本上,是否有一种数字数据类型可以在 Java 中存储长整型、短整型、整数和小数。

更新

以下是我遇到的错误:

BigDecimal a = (BigDecimal)btmpW / imgW;  //不可转换的类型;无法将 'int' 转换为 java.math.BigDecimal

BigInteger a = (BigInteger)btmpW / imgW;  //不可转换的类型;无法将 'int' 转换为 java.math.BigInteger

BigIntegers a = (BigIntegers)btmpW / imgW;  //不可转换的类型;无法将 'int' 转换为 java.math.BigIntegers

BigDecimal a = btmpW / imgW;  //不可转换的类型;要求:BigDecimal,但找到了 int

BigInteger a = btmpW / imgW;  //不可转换的类型;要求:BigInteger,但找到了 int

BigIntegers a = btmpW / imgW;  //不可转换的类型;要求:BigIntegers,但找到了 int
英文:

I have a code in java that says,

btmpW / imgW

but in this case I get 0 as the value, I tried using int, long, float and double as my data type but it still returns 0.

When I try to do the Log.i() on both int values, I got this

I/btmpW: 548
I/imgW: 1041

So using a calculator to calculate, I got 0.5346341463... and I am new to Java, but I think Java don't accept numbers with more than 9 digits, making me assume that Java returns only the (int)0 and since the digits are more than 9, it just ignored the decimal. So i searched and found something like BigDecimal and BigInteger. So I tried to use them, but I am getting some errors

So I want to know, is there a way to store such value in Java. btmpW and imgW are user defined so I don't know what the outcome would be when I run btmpW / imgW, so basically, is there a digits data type in Java that stores both long, short, integer and decimal numbers

Update

Below are the errors I am getting

BigDecimal a = (BigDecimal)btmpW / imgW;  //Inconvertibles types; cannot cast 'int' to java.math.BigDecimal

BigInteger a = (BigInteger)btmpW / imgW;  //Inconvertibles types; cannot cast 'int' to java.math.BigInteger

BigIntegers a = (BigIntegers)btmpW / imgW;  //Inconvertibles types; cannot cast 'int' to java.math.BigIntegers

BigDecimal a = btmpW / imgW;  //Inconvertibles types; REQUIRED: BigDecimal FOUND int

BigInteger a = btmpW / imgW;  //Inconvertibles types; REQUIRED: BigInteger FOUND int

BigIntegers a = btmpW / imgW;  //Inconvertibles types; REQUIRED: BigIntegers FOUND int

答案1

得分: 2

尝试一下:

int btmpW = 548;
int imgW = 1041;

BigDecimal btm = new BigDecimal(btmpW);
BigDecimal img = new BigDecimal(imgW);

System.out.println(img.divide(btm, MathContext.DECIMAL128));

输出:
1.899635036496350364963503649635036

参考文档: https://docs.oracle.com/javase/8/docs/api/java/math/BigDecimal.html#divide-java.math.BigDecimal-java.math.MathContext-

有关 MathContext 的更多信息: https://www.tutorialspoint.com/java/math/java_math_mathcontext.htm

英文:

Try this:

int btmpW = 548;
int imgW = 1041;
		
BigDecimal btm = new BigDecimal(btmpW);
BigDecimal img = new BigDecimal(imgW);
		
System.out.println(img.divide(btm, MathContext.DECIMAL128));

Output:
1.899635036496350364963503649635036

Reference: https://docs.oracle.com/javase/8/docs/api/java/math/BigDecimal.html#divide-java.math.BigDecimal-java.math.MathContext-

For more about MathContext: https://www.tutorialspoint.com/java/math/java_math_mathcontext.htm

答案2

得分: 0

你必须先将这两个数字转换为 BigDecimal。我不确定你的数字是什么类型,但如果是字符串、原始类型(例如 int、double)或原始包装类型(例如 Integer、Double),你可以尝试直接按以下方式转换:

BigDecimal dbBtmpW = new BigDecimal(btmpW);
BigDecimal dbImgW = new BigDecimal(imgW);

之后,你可以使用 divide 方法进行操作。不幸的是,Java 不支持运算符重载,所以你必须调用特定的方法:

BigDecimal result = dbBtmpW.divide(dbImgW);

希望这对你有帮助。

英文:

You have to convert those tow numbers in BigDecimal first. I'm not sure what type does your numbers have, but if it's String, primitive (e.g. int, double) or primitive wrapper (e.g. Integer, Double) you could try to convert them directly as follow:

BigDecimal dbBtmpW = new BigDecimal(btmpW);
BigDecimal dbImgW = new BigDecimal(imgW);

After that you could use divide method to do your operation, unfortunately Java doesn't support operator overloading, so you have to call the specific method:

BigDecimal result = dbBtmpW.divide(dbImgW);

I hope this it'll help you.

答案3

得分: 0

我认为这个技巧使其正常工作了... 我不得不将我的 int 值更改为 double,并且使用 double 进行操作

初始代码

int imgW = imageView.getWidth();
int btmpW = bitmap.getWidth();

最终代码

double imgW = imageView.getWidth();
double btmpW = bitmap.getWidth();
double a = btmpW / imgW;
英文:

I think this trick got it working... I had to change my int values to double, and worked with double instead

Initial code

int imgW = imageView.getWidth();
int btmpW = bitmap.getWidth();

Final code

double imgW = imageView.getWidth();
double btmpW = bitmap.getWidth();
double a = btmpW / imgW;

huangapple
  • 本文由 发表于 2020年4月11日 03:06:13
  • 转载请务必保留本文链接:https://go.coder-hub.com/61147001.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定