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.
Related
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.
Hay,
I want to round a number two digits after the decimal point only by using this mathematic formula:
For example the number 831.38849 should be rounded to 831.39 and printed out. I have tried something like this, but it didn't work: 0.01 * (100 * number + 0.5)
I know that similar questions have been asked before, but I'm not allowed to use all the fancy and easy stuff for this task like Math.round, BigDecimal, etc
⌊ ⌋ are math symbols for the floor function1, 2, 3
So the correct implementation of the mathematic formula
is the following Java code:
Math.floor(100d * number + 0.5) / 100d
Note: 100d can also be written 100.0, 100., or simply 100 (assuming number is a double)
So, the fancy part is handled by casting your floating point number to an integral value. You want to eliminate the portion two places to the right of the decimal, so you first multiply it by 100 to shift the decimal, add the 0.5 to round up anything 0.5 or higher, cast to an int (or a long) to truncate it, then divide by 100 to shift the decimal point back.
[Edit -- Adding condition to handle negatives, so it meets definition of "floor".]
double original_number = 831.38849;
double rounded_number = (original_number >= 0)
? ((long) (original_number * 100 + 0.5)) / 100.0
: ((long) (original_number * 100 - 0.5)) / 100.0;
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.
I have two integer values, x and total. I am trying to find the percentage of x in total as an integer. This is how I am doing it right now:
percentage = (int)((x*100)/total);
The percentage must be an integer. When I do this it always rounds the decimal point down. Is there a simple way to calculate the percentage as an integer so it rounds up if the decimal is .5 or higher?
Use Math.round(x * 100.0/total). But note that this returns a long, so a simple cast to int will be required.
I put 100.0 to force it to use floating point arithmetic prior to the rounding.
(int)Math.round(100.0 / total * x);
should work.
Just use Math.round(100.0 / total * x);
Why not use Math.round((x*100)/total);
Use the standard math library in Java.
percentage = Math.round(*your expression here*);
You can add 0.5 for positive values (round to infitity)
percentage = (int)(x * 100.0 / total + 0.5);
This is faster than using Math.round, but possibly not as clear.
BTW: 100.0 / total * x might not give the same result as x * 100.0 / total as the order of evaluation can change the result for floating point.
Simplest Method -
Question : How to round up value of a decimal number(eg : int n = 4.57)
Answer : Math.round(n);
Output : 5 .