Hey guys, I am trying to round to 3 decimal places.
I used the following code.
this.hours = Round(hours + (mins / 60), 3);
But it's not working.
Where have I gone wrong?
Thanks
You can use this function:
public static double Round(double number, int decimals)
{
double mod = Math.pow(10.0, decimals);
return Math.round(number * mod ) / mod;
}
First thing is that all your variables are int so the result of your division is also an int, so nothing to Round.
Then take a look to: How to round a number to n decimal places in Java
If mins is an integer, then mins / 60 will result in an integer division, which always results in 0.
Try changing from 60 to 60.0 to make sure that the division is treated as a floating point division.
Example:
int hours = 5;
int mins = 7;
// This gives 5.0
System.out.println(Math.round(1000 * (hours + (mins / 60 ))) / 1000.0);
// While this gives the correct value 5.117. (.0 was added)
System.out.println(Math.round(1000 * (hours + (mins / 60.0))) / 1000.0);
if mins is an integer you have to divide through 60.0 to get a floating number which you can round
try using like follow
this.hours = Round(hours + (((double)mins) / 60), 3);
You can't. Doubles don't have decimal places, because they are binary, not decimal. You can convert it to something that does have decimal places, i.e. a base-ten number, e.g. BigDecimal, and adjust the precision, or you can format it for output with the facilities of java.text, e.g. DecimalFormat, or the appropriate System.out.printf() string.
Related
Sorry im new to java, currently i wanted to code the value to next thousand instead of nearest thousand. But i have no ideas how to do it. I tried Math.round but it's for roundest. Please guide me, any help would be appreciated .
Expected output that i looking for :
example 1) if the place values less than 999, it will direct change to 1000
May i know how can i code the math formula for this ?
You can use Math.ceil for this.
// A quick code example :)
int val = 1400;
val = (int) (Math.ceil(val / 1000.0) * 1000);
You need to write some custom code as follow
int leftdigit=value/1000;
int nextthousand=(leftdigit+1)*1000;
Here Kindly note Math.ceil returns double so you should use it properly as stated below as for integer value it won't work properly and integer division will be performed.
double data = 1100;
data = Math.ceil(data / 1000) * 1000;
System.out.println(data);
OUTPUT
2000.0
Conversion from integers to floats leads to chaos, as for the same bit size, float mantissa length will always be smaller than integer size (IEEE-754 float mantissa is 23 bits vs 31 bits for the integer). Converting a large integer to float back and forth will not give the same integer after the conversion.
So here using Math.ceil() may work for integers or small long ints, but will break for large long values (63 bits).
Better use the following code (only works for value > 0):
int ii = ((i - 1) / 1000 + 1) * 1000;
or for long int version:
long ii = ((i - 1) / 1000 + 1) * 1000;
Does not unnecessarily overflow, keep precision even for large values, and probably way faster!
Addenda
As an example, the following java code:
int largeint = Integer.MAX_VALUE - 63;
float fl = (float)largeint;
int largeint2 = (int)fl;
System.out.println(largeint);
System.out.println(largeint2);
Print:
2147483584
2147483647
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
String phrase = "4556.44";
Float num = new Float(phrase);
int dollars = (int) Math.floor(num);
System.out.println(""+dollars);
int cent = (int) Math.floor((num - dollars) * 100.0F);
int cent2 = (int) ((num - dollars) * 100.0);
System.out.println(""+cent+":"+cent2);
This is a Number to Word Class Code Phrase, My Problem is when I run this code fragment the output result is 4556.43. But the input value is 4556.44. Please tell me why this is happened and I need the answer for correct this Problem.
For high precision calculations with controlled rounding use BigDecimal instead Float
Floats are less precise than doubles which are less precise than BigDecimal.
When working with programmes that absolutely must be precise (like financial applications) use a BigDecimal.
If this is a small homework and non-critical app you can try out Double to see if it is precise enough for you.
int cent = (int)(num * 100f % 100f);
Float is not a good choice for precision calculations.
int cent = (int) Math.floor((num - dollars) * 100.0F);
above statement in the code doesn't exactly give you precise answers as you would expect.
Try using Double or BigInteger if possible.
Floats and Doubles cannot represent all possible real numbers, since they use a specification that makes up numbers by adding any of the following numbers: 1, 1/2, 1/4, 1/8, 1/16 etc...
If you try out 0.1f+0.1f+0.1f==0.3f you'll get false. So, as other people said, use BigDecimal, it uses a different representation, it's much slower, but it won't incur in these problems.
You can also do the following
String phrase = "4556.44";
long money = Math.round(Double.parseDouble(phrase) * 100);
long dollars = money / 100;
long cents = money % 100;
System.out.printf("%,d dollars and %d cents%n", dollars, cents);
prints
4,556 dollars and 44 cents
Searching how to make two digit after decimal point as below
$ = (double) (((int)($ * 100)) / 100.0);
Anyone can help to how to make "one digit" after decimal point ?
thanks a lot
What about this:
$ = (double) (((int)($ * 10)) / 10.0);
Note: edited after the comment.
If you need java and rounding instead of truncate try this:
import java.lang.Math;
double a;
a = ((double) (round(a*10))) / 10.0;
This will shift left the dot of one position, round to the nearest integer and then shift the dot back to the right one position.
Edited second time:
What you need is still unclear.
If you are fine with truncate as before:
double $=1.24*(double)amount;
$ = (double) (((int)($ * 1000)) / 1000.0);
outelc.setText("ELC(1.24)= " + Double.toString($) + " /pc");
If you need rounding:
double $=1.24*(double)amount;
$ = (double) ((round($ * 1000)) / 1000.0);
outelc.setText("ELC(1.24)= " + Double.toString($) + " /pc");
If you are using doubles to store/calculate currency values, then you will likely find yourself in a world of pain with rounding. Been there, done that, got the scars.
I highly recommend that you use BigDecimal values for ALL currency values, and do not even involve doubles in the instantiation. Always use the String constructor.
See related questions here and here.
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;
}