How do I round a double away from zero in Java?
The operations I know don't do what I want:
Casting it to (int) rounds it toward zero. (int) 3.7 will be 3,
(int) - 3.9 will be -3.
Math.floor() rounds toward minus infinity. Math.floor(3.7) will be 3.0, Math.floor(-3.9) will be -4.0.
Math.ceil() rounds toward plus infinity. Math.ceil(3.7) will be 4.0, Math.ceil(-3.9) will be -3.0.
Math.round() rounds toward the nearest integer.
However I don't have something that rounds away from zero, such that 3.7 becomes 4.0, and -3.9 becomes -4.0.
Is there such a function in Java?
Check the sign of the number:
double roundedAway = (num >= 0) ? Math.ceil(num) : Math.floor(num)
You can either implement your own function, based on your number being positive or negative, or you can use a RoundingMode. It can round explicitly away from zero with UP
It might look something like this
DecimalFormat df = new DecimalFormat("#.#");
df.setRoundingMode(RoundingMode.UP);
System.out.println(df.format(number_here));
It can be done with RoundingMode and BigDecimal:
double roundAway(double value) {
return new BigDecimal(value).setScale(0, RoundingMode.UP).doubleValue();
}
Note: this will convert the double to a BigDecimal with the precise value of the double. To avoid this, you could use BigDecimal.valueOf(double) instead of the constructor which would use the canonical string representation of the double, but since you're about to round it to a whole number, this would involve an unnecessary conversion to a string. See the constructor documentation for more details.
Related
I am looking for the best way to round to even decimal values. For example I have a double = 4.267833399 and I want to round to the nearest even single decimal place, in this case 4.2 and not 4.3. And 4.3165656 would round to 4.4 not 4.3.
I have searched hard and havent found a simple way to do this.
I would like the result as a double.
Thanks
Assuming you want your answer in a double, this should work:
double result = Math.round(input * 5) / 5d;
You can round to one decimal place with
double d = Math.round(x * 10) / 10.0;
Note this will round 4.26 up to 4.3 as it is the closest, if you want to always round down you can do
double d = (long) (x * 10) / 10.0;
I want to round to the nearest even single decimal place
If you want to round to the nearest multiple of 0.2 you can do
double d = Math.round(x * 5) / 5.0;
This will round 4.299 to 4.2 and 4.3 to 4.4
You can use something like this
double myNumber = 12345.6789;
DecimalFormat df = new DecimalFormat("#.0");
df.format(myNumber);
This would return 12345.7
Edit: Noticed you're looking for rounding to an even number. You could use the above method of rounding and then conditionally manipulate your number after if the decimal is odd.
In java
How to roundoff a value(either float, bigdecimal or double) having the following pattern,
(1) If the value is 1.0, i.e., if the decimal is started with zero then this should not rounded off and the value should be the whole number. ie., in this case "1".
(2) If the value is 1.1 i.e., if the decimal place started with number greater than 0, then the whole number should be rounded to the next number. i.e., if 1.1 then it should be 2.
So you want to round anything below 0.1 down to 0 and 0.1 or more to 1.0
long round = Math.round(x + 0.4);
Try this for a start (for float and double)
int rounded = Math.round(x + 0.4);
See https://docs.oracle.com/javase/8/docs/api/java/math/RoundingMode.html. I believe RoundingMode.CEILING is what you want. BigDecimal lets you control the rounding:
new BigDecimal(1.0).setScale(0, RoundingMode.CEILING).doubleValue(); => 1.0
new BigDecimal(1.1).setScale(0, RoundingMode.CEILING).doubleValue(); => 2.0
Guava includes some utility classes for rounding floats and doubles directly with RoundingModes.
DoubleMath.roundToInt(1.0, RoundingMode.CEILING); => 1
DoubleMath.roundToInt(1.1, RoundingMode.CEILING); => 2
Edit: Whoops. I missed the part where rounding 1.01 should result in 1. The other suggested methods are more correct.
Your specification is not entirely clear to me. I understand your question to mean the "ceil" function, that is, 1.01 has to be rounded up, but your question can also be interpreted such that 1.01 has to be rounded down. (If the latter is what you want, look at Peter Lawrey's answer.)
For doubles (and floats) Java provides the standard method Math.ceil(double a) for the ceil function.
For BigDecimal values you can use the setScale method: set the scale to 0 (no decimal fraction) and the rounding mode to RoundingMode.CEILING to specify how to round:
static BigDecimal ceil(BigDecimal a) {
return a.setScale(0, RoundingMode.CEILING);
}
How to round up a decimal number to a whole number.
3.50 => 4
4.5 => 5
3.4 => 3
How do you do this in Java? Thanks!
With the standard rounding function? Math.round()
There's also Math.floor() and Math.ceil(), depending on what you need.
You can use
int i = Math.round(f);
long l = Math.round(d);
where f and d are of type float and double, respectively.
And if you're working with only positive numbers, you can also use int i = (int)(d + 0.5).
EDIT: if you want to round negative numbers up (towards positive infinity, such that -5.4 becomes -5, for example), you can use this as well. If you want to round to the higher magnitude (rounding -5.4 to -6), you would be well advised to use some other function put forth by another answer.
Java provides a few functions in the Math class to do this. For your case, try Math.ceil(4.5) which will return 5.
new BigDecimal(3.4);
Integer result = BigDecimal.ROUND_HALF_UP;
Or
Int i = (int)(202.22d);
Using Math.max you can do it like this:
(int) Math.max(1, (long) Math.ceil((double) (34) / 25)
This would give you 2
how to keep only 3 value after point in BigDecimal ?
i found a solution but it requirt a RoundingMode
BigDecimal d = BigDecimal.valueOf(0.5649);
System.out.println(d.remainder(BigDecimal.ONE));
System.out.println(d.remainder(BigDecimal.ONE).divide(BigDecimal.valueOf(1), 3, RoundingMode.CEILING));
i want to keep a number exact without rounding.
Just use setScale and RoundingMode.DOWN
Rounding mode to round towards zero. Never increments the digit prior
to a discarded fraction (i.e., truncates).
for example
BigDecimal.valueOf(0.5649).setScale(3, RoundingMode.DOWN)
BigDecimal isn't intended to be used with such limitations. Only ever wanting 3 decimals is a strange requirement. Maybe you only want to present 3 decimals to the users?
If so I suggest using: java.text.DecimalFormat
If you really want to make sure that you never do calculations with higher precission than 3 decimals I suggest making your own reprensentation/class. Internally you hold the value as a long (or appropriate class/primitive) but at a value 1000 times the actual value. All calculations are done with the internal reprensentation and when asked for a value, divide the internal value with 1000.0d and convert to double (or appropriate) and return.
I would use double for this.
double d = 0.5649;
d = (long) (d * 1000) / 1000.0; // round down.
System.out.println(d);
prints
0.564
or
d = (long) (d * 1000 + 0.5) / 1000.0; // round half up.
BigDecimal numerator = new BigDecimal(numerator);
BigDecimal denominator = new BigDecimal(denominator);
double result = numerator.divide(denominator).doubleValue();
Division on certain conditions results in a zero at the end (e.g. 0.0060).
Dividing equal numbers results in a zero at the end (e.g 1.0).
I would prefer to trim the trailing zero in both cases. How should I do this?
How about keeping the result as a BigDecimal and then you can set the scale on it to only represent the significant figures that you want.
An easy way to do this, for some numbers, is to use BigDecimal#stripTrailingZeros(). However, if the number is an integer with trailing zeros you'll get an engineering representation e.g. 600.0 will give you 6E+2. If this isn't what you want, you'll have to detect this condition and manually use BigDecimal#setScale() to set the scale appropriately.
If you need to keep to a restricted maximum number of decimal digits you'll need to use alternative formatting/rounding mechanisms before applying this technique.
It's also a good idea to only do this on values that you're going to display, not on the internal values of your model. Treat it as a view/presentation layer modification.
If you must convert to a double, then it's only the formatted representation you can alter. In this case, if you've got a variable number of decimal places that you want to format to, I'd just drop it into a string/character array, scan backwards for the first non-zero character and truncate it there. Not the most performant means, but simple and reliable.
You could even use a regex for this purpose.
you can do this by using DecimalFormat. something like:
DecimalFormat df = new DecimalFormat(".0");
double formatResult = df.format(result);
will create something of 1.0 if the result is 1.278494890. there are many possible patterns that could be used here
Ok, so you've got this code:
BigDecimal numerator = new BigDecimal(numerator);
BigDecimal denominator = new BigDecimal(denominator);
double result = numerator.divide(denominator).doubleValue();
and you want more control over your output. Since result is a double, which is a primitive, you won't have much control.
From my understanding, you don't want to do any rounding to n decimal places, you want original precision paired with desired formatting.
You have few options.
BigDecimal numerator = new BigDecimal(numerator);
BigDecimal denominator = new BigDecimal(denominator);
BigDecimal div = numerator.divide(denominator);
If you stay with BigDecimal, your output will be better. If you put 10 as the numerator and denominator in above code, System.out.println(div) will yield 1.
Generally, be careful of using above code because some combinations of numerator and denominator will throw
java.lang.ArithmeticException: "Non-terminating decimal expansion; no exact representable decimal result."
If you want to avoid such situations, and not worry about precision beyond double's internal representation, use double directly.
System.out.println(2312 / 2.543); //909.1624066063704
System.out.println(1.0 / 1.0); //1.0
System.out.println(1 / 1); //1
When using double numbers, you might get a 0 at the end, such as 0.0060 in your case. If you want to be sure what you're getting, you'll have to convert your result to a String using
String dec = String.valueOf(10.0/10.0); //1.0
and then using
String newDec = dec.endsWith("0") ? dec.substring(0, dec.length() - 1) : dec;
to eradicate that last 0. Of course, if your string ends with .0, you have a choice based on your preferences whether you want to leave that leading . or not.