java中如何划分两个long型变量

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24650871/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-14 13:47:23  来源:igfitidea点击:

How to divide two long variables in java

javavariablestypescasting

提问by Arulmurugan

Sorry for the basic question, I have to divide the long variable by another long variable, but it returns 0. Can any one help

对不起,基本问题,我必须将 long 变量除以另一个 long 变量,但它返回 0。任何人都可以帮忙

long a = 3004230;
long b =  6793368;
long c = (a/b)*100;

回答by Rudi Kershaw

Literal Values and Literal Arithmatic

文字值和文字算术

There are a couple of issues with that code. Firstly, non-floating point literal valuesare of type intby default and so 3004230in your code is an int. To explicitly declare it a longuse 3004230Linstead.

该代码有几个问题。首先,默认情况下非浮点文字值是类型int的,因此3004230在您的代码中是int. 显式声明它是一种long用途3004230L

Also, all arithmetic done with non-floating point literalsreturns an intresult unless casted specifically to a floating point type such as floator double. As such (a/b)*100is less than 1, and therefore is truncateddown to 0 (the floating point values are just cut off). Also, even if it did return the same result you are trying to store it in a longwhich can not store floating point values.

此外,所有使用非浮点文字进行的算术运算都会返回int结果,除非专门转换为浮点类型,例如floator double。因此(a/b)*100小于 1,因此被截断为 0(浮点值被截断)。此外,即使它确实返回了相同的结果,您也试图将其存储在long无法存储浮点值的 a 中。

So, you should do something like the following to get the real result.

因此,您应该执行以下操作以获得真实结果。

long a = 3004230L; // Use the L notation to declare this literal a long.
long b = 6793368L;
double c = ((double)a/b)*100; /* casting your division result to (double) means 
                                 the result will not be 0 */

I hope this helps.

我希望这有帮助。

回答by Cengiz

final long a = 3004230;
final long b = 6793368;
final double c = ((double) a / b) * 100;

=> c = 44.22298335670907

=> c = 44.22298335670907

回答by Dawnkeeper

What you are doing right now is integer division. This will always return an integer/long result. You have to use a float or double to get a floating point result even if you cast it back to integer values afterwards.

你现在正在做的是整数除法。这将始终返回整数/长结果。您必须使用 float 或 double 来获得浮点结果,即使之后将其转换回整数值也是如此。

long c = (long)(a/(float)b*100);

回答by whhotw

try it.

尝试一下。

long a = 3004230;
long b =  6793368;
long c = ((long)((float)a/(float)b)*100); //answer=44
float c = ((long)((float)a/(float)b)*100); //answer=44.1256378

回答by sona

The long c you are creating isn't the value you expected. It is in the integer range. To create longs, use

您创建的 long c 不是您期望的值。它在整数范围内。要创建多头,请使用

final long c= 3004230L * 6793368L; By adding an L after the number literal, you tell the compiler to compile it as a long instead of an int.

最终长 c= 3004230L * 6793368L;通过在数字文字后添加 L,您可以告诉编译器将其编译为 long 而不是 int。

回答by rock321987

obviously the answer will be 0for above..as you can see when you divide

显然,答案将在0上面......正如你在划分时所看到的

3004230 / 6793368 = 0.442 = 0(when casted to longtype)

3004230 / 6793368 = 0.442 = 0(当转换为long类型时)

and

0 * any number = 0..

0 * 任何数字 = 0..

to convert it use this

转换它使用这个

double c = (a * 1.0/b)*100.0;

you have to use datatype that can store decimalvalue which is floator double..longcannot store decimal numbers

你必须使用可以存储decimal值的数据类型,float或者double..long不能存储十进制数

回答by vz0

You can do the same without casting to a float:

您可以在不强制转换为 a 的情况下执行相同操作float

long a =  3004230L;
long b =  6793368L;
long c = (a * 100L)/b;