英文:
What datatype should i use,if result coming in both decimal and non decimal
问题
我从数据库接收到整数类型的分数。在接收到分数后,我进行一些计算并向用户显示结果。
假设我接收到了20、21和22。我执行21/1.5+20+22的计算,结果为43.50,但有时接收到的分数都是偶数,结果将会是50.00。
问题是,如果小数部分不为00,我想显示43.50,如果小数部分为00,我想显示50。我应该使用什么数据类型进行计算和显示结果,如我上面所述。谢谢。
目前我正在使用这个,但使用整数类型不会显示小数部分:
int tot = a1 + a2 + a3 + a4 + a5 + a6 + a7 + a8 + a9 + a10 + a11 + a12 + a13 + a14 + a15 + a16 + a17 + a18 + a19 + a20 + a21 + a22 + a23 + a24;
英文:
I am receiving points from database that is integer. After receiving points i am doing some calculation and showing result to user.
Assume i received 20, 21, ,22. I am doing 21/1.5+20+22 which results in 43.50, But some time the points i receives is all even so the result will be like 50.00.
The Problem Is i want to show 43.50 if decimal is not 00 And 50 If decimal is 00..
What data type should i use for calculation and show results like i mentioned above.THANKS.
Currently I am using this but using integer wont show the decimal
int tot = a1+a2+a3+a4+a5+a6+a7+a8+a9+a10+a11+a12+a13+a14+a15+a16+a17+a18+a19+a20+a21+a22+a23+a24;
答案1
得分: 1
将输出保存为双精度数据类型,在显示时进行如下检查:
Double result;
if (result % 1.0 == 0.0) {
// 显示 result.intValue();
} else {
// 显示 result System.out.printf("result : %.2f%n", result);
}
英文:
Save the output in double datatype, and while showing put checks on it like this:
Double result;
if (result%1.0 == 0.0){
// show result.intValue();
} else {
// show result System.out.printf("result : %.2f%n", result);
}
答案2
得分: 0
如果您只想保留两位小数进行展示,您可以使用 DecimalFormat,例如,对于计算浮点值,您可以使用 double 或 float 类型:
DecimalFormat df = new DecimalFormat("#,##0.00");
String formatedTot = df.format(tot);
System.out.println(formatedTot);
英文:
If you just want to present with exactly 2 decimal places you can use DecimalFormat e.g. and for calculating float point values you can use double or float type
DecimalFormat df = new DecimalFormat("#,##0.00");
String formatedTot = df.format(tot);
System.out.println(formatedTot);
答案3
得分: 0
从描述中很难为您指明正确的方向。
请展示一些代码,说明您如何在Android中显示结果。
关于数据类型,应该明确一点,您不能将整数转换为小数,因为您不能神奇地将小数值添加到整数中。因此,我猜您需要一个小数数据类型,并需要检查是否是整数,可以在这里找到解释:https://stackoverflow.com/q/9898512
英文:
From the description it's hard to point you in the correct direction.
Please show some code how you show the results in Android.
Regarding datatype it should be clear that you can't convert an integer
to a decimal
because you can't magically add decimal values to it. Therefore I guess you need a decimal
datatype and need to check if it's an integer
which is explained here: https://stackoverflow.com/q/9898512
答案4
得分: 0
你的问题不在于计算,而在于显示结果。当你在计算中使用 int
或 Integer
时,Java 会始终向下取整。甚至 float
和 double
也只是近似值(见此处)。根据你需要的精度,可以使用 float
(中等精度)、double
(可接受的精度)或 BigDecimal
(最佳精度)。
要解决你的问题,需要根据需要精确计算你的数字,并在打印时定义它应该如何表示,详见此处的说明。
英文:
Your issue is not in calculation, but in displaying the result. When you use int
or Integer
for your calculation, Java will always round down. Even float
and double
are only approximations (see here). Depending on how accurate you result should be use float
(medium accuracy), double
(ok accuracy) or BigDecimal
(best accuracy).
To solve your issue, calculate your number as precise as required and define while printing how it should be represented as described here.
答案5
得分: 0
你可以使用 double
数据类型或 float
数据类型。如果你需要检查小数点后的余数,可以使用类似以下的代码:
double num = 50.00;
if (num % 1.0 == 0.0) {
int numOne = (int) num;
System.out.println(numOne);
}
上面的代码(由Rajat Mehra创建,我进行了编辑)会将余数为 "0" 的值转换为整数并相应地显示它们。
英文:
You could use double
data type or float
data type. If you need to check the remainder after the decimal point you could use something like,
double num = 50.00;
if (num % 1.0 == 0.0){
int numOne = (int) num;
System.out.println(numOne)
}
The above code (by Rajat Mehra) which I edited, will cast values with "0" remainder to an integer and display them accordingly.
答案6
得分: 0
你的输入是一组整数。你的问题暗示你不想进行四舍五入,所以你的计算结果应该是一个浮点数或双精度数。
double result = calculate(integers);
你提到你想显示计算值的小数部分,即使结果是一个整数。Java 有多种选项可以格式化输出。一个简单的方法是使用 printf。
System.out.printf("result=%.2f%n", result);
英文:
Your input is a set of integers. Your question implies you do not want rounding so the result of your calculation should be a float or double.
double result = calculate(integers);
You note that you want to show the fractional portion of the calculated value even if the result is an integer. Java has a variety of options to format the output. One simple approach is using printf.
System.out.printf("result=%.2f%n", result);
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论