简体   繁体   English

小于1且大于0的值的计算百分比

[英]Percentage Calculation for values less than 1 and greater than 0

I am trying to display the percentage using BigDecimal. 我试图使用BigDecimal显示百分比。

for example if i have to do 例如,如果我必须这样做

    double val = (9522 / total ) * 100;

    System.out.println("Val is :" + val);

where total is 1200000. 总计120000。

I want to display the calculation as a percentage but since the calculation comes to a value less than 0 , I am unable to display the percentage 我想将计算显示为百分比,但由于计算结果为小于0的值,我无法显示百分比

I also tried 我也试过了

    BigDecimal decimal = new BigDecimal((9522 * 100 ) / total);
    BigDecimal roundValue = decimal.setScale(2, BigDecimal.ROUND_HALF_UP);
    System.out.println("decimal: " + decimal);
    System.out.println("roundValue: " + roundValue);

but with the same result. 但结果相同。

How can I display the percentage even if it is in Decimals? 即使是小数,我怎么能显示百分比?

The problem is you are doing integer division. 问题是你正在进行整数除法。

If you want fractional results, you need to do floating point division. 如果需要小数结果,则需要进行浮点除法。

double val = (9522 / (double) total) * 100;

Casting one of your operands to a double will cause Java to perform the correct type of division instead of defaulting to integer division. 将一个操作数强制转换为double将导致Java执行正确的除法类型,而不是默认为整数除法。

You have to inform Java that you want at least one of the numerator or denominator treated as a double, to make sure the result is a double. 您必须通知Java您希望至少将分子或分母中的一个视为double,以确保结果为double。

This will work: 这将有效:

double val = ((double)9522 / total ) * 100;

System.out.println("Val is :" + val);

BigDecimal decimal = new BigDecimal((9522 * 100 ) / total); BigDecimal decimal = new BigDecimal((9522 * 100)/ total);

This is not how you do operations on BigDecimal : by the time the BigDecimal is constructed, the precision is gone, because the calculation (9522 * 100 ) / total is done at compile time. 不是你在BigDecimal上进行操作的方式:在构造BigDecimal ,精度已经消失,因为计算(9522 * 100 ) / total是在编译时完成的。 That's why the result is the same as with integers: in fact, the entire calculation is done in integers. 这就是为什么结果与整数相同的原因:实际上,整个计算是用整数完成的。

Here is how you calculate with BigDecimal objects: 以下是使用BigDecimal对象计算的方法:

BigDecimal decimal = new BigDecimal(9522)
    .multiply(new BigDecimal(100))
    .divide(new BigDecimal(total));

If you divide two integer you will get the result truncated. 如果除以两个整数,则会截断结果。 Add a .0 so that it is converted to floating point, and then you will not get a truncated result 添加.0以使其转换为浮点,然后您将不会得到截断的结果

new BigDecimal((9522.0 / total) *100);

You may be missing a cast. 你可能会错过演员。

double val = (9522 / (double)total ) * 100;

System.out.println("Val is :" + val);

My suspect is that total is an int, and hence 9522/1200000 results in an integer, which is truncated to 0 because the operation implies that the result must be smaller than 1. If you convert total to double the result is going to be a double, and you will be able to retain the decimals. 我的怀疑是total是一个int,因此9522/1200000会产生一个整数,由于操作意味着结果必须小于1,因此将其截断为0。如果将total转换为double,结果将是加倍,你将能够保留小数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM