Reputation:
I'm new to Java, and I'm trying to make my numbers look good, so I want to cast doubles to ints, but only if the double is something like 3.0, something that doesn't actually lose any decimals. How do I check whether there is a decimal in a double?
My program contains a whole lot of code, but what I have is a loop that assigns a series of values to indices of an array, and then I want to sequentially print each value, losing the decimal if I can.
Sorry I can't post code, but there's a whole lot and a little snippet probably wouldn't make sense.
Thanks, Harold
Upvotes: 0
Views: 83
Reputation: 718788
It is simple to test if a floating point number is "close" to an integer ... for some definition of close; see @hexafraction's answer.
But I think think you are going about this in the wrong way:
If you are going to prettify the numbers in some unusual way, then you should be doing it in the formatting; i.e. the conversion of numbers to strings.
There might be a flavour of Formatter
or DecimalFormat
that does this for you already. (See @Cody's answer for example.)
Otherwise, you could create a simple subclass of DecimalFormat
that rewrites the output string to remove redundant (to your way of thinking!) trailing zeros, etc.
I also would question that it is sound practice to prettify numbers like this. To a mathematician (and indeed anyone who understood their primary school maths classes), 1
and 1.0
mean different things.
1
means either the integer one, or a number with no digits of precision after the decimal point; i.e. a number in the range 0.5
to 1.49999...
.
1.0
means NOT an integer but a number in the range 0.95
to 1.049999...
.
So your "tidying up" is actually throwing away useful information. To someone who might actually understand and care what the numbers mean, this would be objectionable. Of course, it depends on the context, and the importance / significance of the numbers.
The OP then comments:
Come to think of it, it would probably be better to just display them without the .0, since I'll be doing algebraic calculations with them later.
Spot on! If you change the numbers to make them give pretty output, you are compromising their accuracy. That is going to compromise the accuracy of follow-on calculations using those numbers.
Upvotes: 2
Reputation: 6017
If you just want to format the numbers for output, you should look into PrintStream#printf. Here's a good overview of number formatting
System.out.printf("%.0f", "42.0357");
System.out.printf("%.3f", "42.0357");
System.out.printf("%5.0f", "42.0357");
This will print out
42
42.036
42
These specifiers give you a lot of control over spacing, alignment, decimals, etc. Give them a try.
Upvotes: 1
Reputation: 41281
You can assign a reasonable error amount and compare it:
if(Math.abs(someDouble-(StrictMath.round(someDoublet))<0.0001){
long thatLong = StrictMath.round(someDouble);
}
This compares the float to that float rounded, and if it's smaller than 0.0001(or a delta of your choice) casts to an int.
Upvotes: 1