How do I convert numbers from double to int without rounding?

I want to calculate how many columns can fit in the container First, I calculate how many columns can fit, and then calculate their margins After that, I check whether they fit the margins, and if not - reduce the number of columns

int columnMargin = 50;
double result = columnContainerH@R_637_2419@/minimalColumnWidth;
int columnCount = (int) result; // here's the problem
int numberOfMargins = columnCount - 1;
double finalCount = columnContainerH@R_637_2419@/minimalColumnWidth*columnCount+columnMargin*numberOfMargins;
if(finalCount<1){
    columnCount--;
}

The problem is that I don't know how to convert from double to int without rounding. I just need to get rid of all the numbers after decimal I've tried (int) double, (int) math Floor (double) and new decimalformat ('#') format(double). As a double, I took 1.999999999999999999999 All the above convert it to 2 Ideal results – 1;

My double will be completely random It can be 1.0, 1.999999999, 2.45, 3.5959547324, etc

What I need is to get a complete part and completely discard everything after decimals

Solution

You can do this by converting to int, because you've done it in the code you're trying

I know you are not satisfied because you think you have found a case when you try to round rather than discard the decimal part

You have tried to use the value 1.99999999999999999999999999 and you have noticed that converting it to int produces an int with a value of 2. From there, you conclude that the conversion does not just discard the decimal part, but rounds it to the nearest integer 2

Your conclusion is incorrect The number 2 was not obtained due to conversion to int The number 2 is the result of the compiler parsing the written text value 1.99999999999999999999999999 Doubles have no infinite precision This means that you can't write out how many decimals you want and expect it to be saved correctly in the double value Doubles only provide the approximate value you require Therefore, when you type a literal value of 1.99999999999999999999999, the compiler knows that it cannot accurately represent the value, but takes it as the closest value that can be represented in double

The representable value closest to 1.999999999999999999999999 is 2 As far as the java compiler is concerned, these two numbers are the same

So when you write:

double d = 1.99999999999999999999d;

The java compiler equates it exactly to:

double d = 2d;

You'll notice that at this point, you haven't tried to delete the decimal, just keep the whole part You only declare a value for your double, and for you, the value is likely to have a decimal part

You can only attempt to retain the entire value if:

int i = (int)d;

The decimal is deleted here. Your int only contains all the contents contained in the double value

However, in your example, since your double value is 2.0, the whole part is taken as 2.0 This is not rounding This is not just keeping the whole part

The correct way to delete decimals and keep only the whole part is to convert to int

(double precision cannot be used if it is important to be able to manipulate values such as 1.9999999999999999999999 and they are different from 2.0. They do not have enough precision. You should use BigDecimal and use the new BigDecimal ("1.99999999999999"). The constructor must be called using string instead of floating-point values because floating-point values cannot represent the desired value.)

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>