I have the following scenario , let us assume 125,250,500 are rounding parameter based on which we have to do rounding.
when the rounding parameter is 125 then I need to round off to 1/8 th dollar,
ie, value = 10.01 then it should return 10.125
value = 110.2 then it should return 110.250
when the rounding parameter is 250 then I need to round off to quarter dollar,
ie, value = 10.01 then it should return 10.250
value = 110.3 then it should return 110.500
when the rounding parameter is 500 then I need to round off to half dollar,
ie, value = 10.01 then it should return 10.500
value = 110.6 then it should return 111.
I have written code using Math.round(8f * value/8f) which rounds of to nearest 1/8 th dollar but the rounding should be UP always but in my case 10.01 rounds off to 10
First off, you're not rounding, you're doing a ceiling.
This code does what you want:
Math.ceil(n * 1000 / factor) * factor / 1000;
Here is the code bundled as a method:
public static double ceil(double n, int factor) {
return Math.ceil(n * 1000 / factor) * factor / 1000;
}
Note that no casting is required, because in java arithemtic if one of the operands is double, the others are automatically (and safely) widened to double too.
I have written code using Math.round(8f * value/8f) which rounds of to nearest 1/8 th dollar
No it doesn't. It rounds to the nearest integer.
Try for example Math.round(((8f+1/8f) * value))/8f.
Break it down to manageable steps, no value trying to do it on one line. So:
How many 1/8th dollars are there in value?
float dollar8ths = value * 8f;
Round that up:
int dollar8thsRoundedUp = (int) Math.ceil(dollar8ths);
Finally, put back into dollars:
float dollars = dollar8thsRoundedUp / 8f;
This should do what you want:
double myRound(double val, int param)
{
val = 1000 * val / param;
val = Math.ceil(val);
return val * param / 1000;
}
e.g.:
System.out.println(myRound(10.01, 125));
gives
10.125
Related
I need to cast a double to an int in Java, but the numerical value must always round down. i.e. 99.99999999 -> 99
Casting to an int implicitly drops any decimal. No need to call Math.floor() (assuming positive numbers)
Simply typecast with (int), e.g.:
System.out.println((int)(99.9999)); // Prints 99
This being said, it does have a different behavior from Math.floor which rounds towards negative infinity (#Chris Wong)
To cast a double to an int and have it be rounded to the nearest integer (i.e. unlike the typical (int)(1.8) and (int)(1.2), which will both "round down" towards 0 and return 1), simply add 0.5 to the double that you will typecast to an int.
For example, if we have
double a = 1.2;
double b = 1.8;
Then the following typecasting expressions for x and y and will return the rounded-down values (x = 1 and y = 1):
int x = (int)(a); // This equals (int)(1.2) --> 1
int y = (int)(b); // This equals (int)(1.8) --> 1
But by adding 0.5 to each, we will obtain the rounded-to-closest-integer result that we may desire in some cases (x = 1 and y = 2):
int x = (int)(a + 0.5); // This equals (int)(1.8) --> 1
int y = (int)(b + 0.5); // This equals (int)(2.3) --> 2
As a small note, this method also allows you to control the threshold at which the double is rounded up or down upon (int) typecasting.
(int)(a + 0.8);
to typecast. This will only round up to (int)a + 1 whenever the decimal values are greater than or equal to 0.2. That is, by adding 0.8 to the double immediately before typecasting, 10.15 and 10.03 will be rounded down to 10 upon (int) typecasting, but 10.23 and 10.7 will be rounded up to 11.
(int)99.99999
It will be 99.
Casting a double to an int does not round, it'll discard the fraction part.
Math.floor(n)
where n is a double. This'll actually return a double, it seems, so make sure that you typecast it after.
This works fine int i = (int) dbl;
new Double(99.9999).intValue()
try with this, This is simple
double x= 20.22889909008;
int a = (int) x;
this will return a=20
or try with this:-
Double x = 20.22889909008;
Integer a = x.intValue();
this will return a=20
or try with this:-
double x= 20.22889909008;
System.out.println("===="+(int)x);
this will return ===20
may be these code will help you.
Try using Math.floor.
In this question:
1.Casting double to integer is very easy task.
2.But it's not rounding double value to the nearest decimal. Therefore casting can be done like this:
double d=99.99999999;
int i=(int)d;
System.out.println(i);
and it will print 99, but rounding hasn't been done.
Thus for rounding we can use,
double d=99.99999999;
System.out.println( Math.round(d));
This will print the output of 100.
First off this is the code I use :
public static float roundAt(float value , int digits) {
double pow = Math.pow(10, digits);
double retvalue;
retvalue = (value * pow);
retvalue = Math.round(retvalue);
retvalue = retvalue / pow;
return (float) retvalue;
}
So using this method I do the following
if I round these values:
roundAt(0.495f,2) = 0.5
roundAt(1.495f,2) = 1.5
roundAt(2.495f,2) = 2.49
I try to understand the logic behind this but I can't. I want the rounding to act the same everytime so I would get 2.5 with roundAt(2.495f,2). Any idea what's going on ?
Thank you
The reason for that is, that you calculation...
retvalue = (value * pow);
...leads to...
2.495 * 100.0 = 249.4999885559082
Using a double instead of a float will fix that specific problem. Neither float nor double are exact values, but float is even less exact than double and will lead to errors more easily. If you are curious, you will find much material on floating point arithmetics out there.
If you do 2.495f * 100.0 you will notice that the result is 249.4999885559082. Since the decimal part is less than .5, this gets rounded down to 249.0 that, when divided by 100.0, gives 2.49.
If you want more information on how to round a double, check this other answer.
So it's easy to round the tenths place with just doing something like:
int y;
double x = 2.5;
y = (int) (x+.5);
but how would you go about rounding to the hundredths or even thousands place without using Math.round()?
You can multiply the original number by a power of 10 so that the desired place to round is in the unit's place, apply the "add half and round" method you already have, then divide the same power of 10, so the resulting number is now back to the original scale.
For hundredths:
Declare y to be a double. This is so that rounding 2.125 to the hundredths' place will result in 2.13, not 2.
Multiply the x value by 100.0.
Add 0.5.
Cast to int. (Or long for more precision.)
Divide by 100.0.
Ex.: Rounding 2.125 to the hundredths' place.
1. 2.125 * 100.0 is 212.5.
2. 212.5 + 0.5 is 213.0.
3. 213.0 cast to int is 213.
4. 213 divided by 100.0 is 2.13.
For thousandths, the procedure is the same, except that 100.0 is replaced by 1000.0.
The above method is subject to floating-point errors due to the finite precision of the double floating-point type.
You can also convert your value to a BigDecimal. Then you can use BigDecimal's round method. It takes a MathContext that allows you directly to round to the desired precision.
public class Decimal {
public static void main(String[] args) {
double xd = 2.125;
double mult = xd * 100.0;
double add = mult + 0.5;
int reuslts = (int) add;
double result = reuslts / 100.0;
System.out.println(result);// 2.13
}
}
This is the code i am using to calculate a percentage and round it to 2 decimal places. However, at the moment, the result comes out as 45.0% rather than 45.33%
int one = 432;
int rolls = 953;
double test1 = 100 * one / rolls;
double finalValue1 = Math.round( test1 * 100.0 ) / 100.0;
Why are no decimal places showing?
as you are multiplying integers the result of test1 is integer
so you have to say
double test1= 100.0*one/rolls; or
double test1=(double)100*one/rolls
You could use String.format("%.2f",test1). If you use round, then java would round the the integer value. Thus, formatting it this way would give you the answer you seek.
The problem is with the following line:
double test1 = 100 * one / rolls;
That is because 100, one, and rolls are of type int.
When you do 100 * one, that will result in 43200. Then, we have to execute the rest of the calculation... 43200 / 953 would actually equal 45.330535152, but because these are both of type int the result will be 45. Making test1 = 45. Since test one is a double, it will actually be 45.0.
The next calculation, which uses test1 will be off because of the above, resulting in "no decimal value".
To fix this, you can change the type of one and rolls to double.
You have two problems.
First and foremost, you are performing integer division
double test1 = 100 * one / rolls;
100, one and rolls are all int. This means the result is an integer, regardless of what you've declared the return type to be. This is covered in the SO question Why the result of 1/3=0 in java?:
the result variable as double just causes an implicit conversion to occur after division.
If you want doubles, use doubles:
double one = 432.0;
double rolls = 953.0;
After fixing that, your division of Math.round( test1 * 100.0 ) / 100.0; will produce a double, but probably with more than two places of precision. It's unclear at that point if you want further rounding to a specific precision, but if you only wanted to print the two digits after the decimal you could use:
System.out.printf("%.2f", finalValue1);
I have a value like this:
421.18834
And I have to round it mathematical correctly with a mask which can look like this:
0.05
0.04
0.1
For example, if the mask is 0.04, i have to get the value 421.20, because .18 is nearer at .20 than .16.
All functions that I found using Google didn't work.
Can you please help me?
double initial = 421.18834;
double range = 0.04;
int factor = Math.round(initial / range); // 10530 - will round to correct value
double result = factor * range; // 421.20
You don't need a special function. You multiply your original number by (1/mask), you round it to a decimal and you divide again by the same factor.
Example with 0.05
factor = 1/0.05 = 20
421.18834 * 20 = 8423.7668
int( 8423.7668 ) = 8424
8424.0 / 20.0 = 421.20
Example with 0.01
factor = 1/0.1 = 10
421.18834 * 10 = 4211.8834
int( 4211.8834 ) = 4212
4212.0 / 10.0 = 421.20
Contrary to all the answers you will probably get here about multiplying and dividing, you can't do this accurately because floating point doesn't have decimal places. To need to convert to a decimal radix and then round. BigDecimal does that.
Both fredley and Matteo make the assumption that the rounding factor is itself a factor of 100. For factors like 0.06 or 0.07, this is an incorrect assumption.
Here's my Java routine:
public double rounded(double number, double factor) {
long integer = (long) number;
double fraction = number - integer;
double multiple = (fraction / factor);
multiple = Math.round(multiple);
return factor * multiple + integer;
}