This question already has answers here:
How to round a number to n decimal places in Java
(39 answers)
Double decimal formatting in Java
(14 answers)
Closed 6 years ago.
Suppose, I have a double value.
double dble = 5.91742691
Now can I get that value with two digits after the point. I mean to say,can I get 5.91 from there programmatically?
One more, suppose, I want to get an integer from a double value if the double value is X.9XXXXX. Here I mean to say, I want to compare the AFTER-POINT value. For your understanding here dble's AFTER-POINT value is 91742691. How can I do that?
If you want to truncate a double to 2 decimal places, do
double twoDecDbl = (int)(dble * 100) / 100.0; // 5.91
or
double twoDecDbl = Math.floor(dble * 100) / 100; // 5.91
However, if you want to get the numbers after the decimal place as an integer (which I don't know why you would want to do this), then do
long decimals = Long.parseLong(("" + dble).split("\\.")[1]); // 91742691
Note: The maximum value of an int is 2,147,483,647 (10 digits there), but a double can hold 16 digits after the decimal point, so a long must be used in order to stay safe (can have 19 digits). Alternatively, you can just keep it as a String by removing the wrapping parse.
For #1 Try it
NumberFormat formatter = new DecimalFormat("#0.00");
String s = formatter.format(dble);
double num = Double.valueOf(s);
or
dble = Math.floor(dble * 100) / 100;
For #2 Try it
double num = dble - (int)dble;
In case Integer.MIN_VALUE < dble < Integer.MAX_VALUE
Related
This question already has an answer here:
How to convert from a double without a fractional part to a hexaicosadecimal string (base-26)?
(1 answer)
Closed 1 year ago.
What is the way to convert from a double with or without a fractional part to a hexaicosadecimal string? What are some tips or possible shortcuts?
For any base B, multiply the factional decimal number part by B.
If the number is >= 1, assign to an int and convert to 0-F as appropriate.
save only the previous fraction and continue from the beginning.
Here is an example for binary.
double d = .2342;
System.out.print(".");
for (int i = 0; i < 20; i++) {
d = 2*d;
int bit = (int)d;
System.out.print(bit);
d = d-bit;
}
System.out.println();
Prints the following for .2342 with 20 bits of accuracy
.00111011111101001000
Note that since floating point products of fractions can degrade, care must be taken for long strings. And unless the fraction is a power of.5, it will be repeating.
This question already has answers here:
Always Round UP a Double
(8 answers)
Closed 7 years ago.
Is there a way to automatically round up a value in java?
For example:
//generate a random integer value
int randomVal = RandomHelper.nextIntFromTo(1, otherVal);
/* then divide the integer value in half... the error I am getting is that its a double, probably
because the number generated isn't an even number, but I NEED it to be an integer. Can I round up?*/
int value = randomVal * 0.5;
You can add 1 then divide by 2 instead of multiplying by 0.5. That way, you avoid a floating point operation followed by a conversion to int.
int value = (randomVal + 1) / 2;
Use Math.ceil():
int value = (int)Math.ceil(randomVal * 0.5);
This question already has answers here:
How to round a number to n decimal places in Java
(39 answers)
Closed 8 years ago.
I have a application which reads SAS xpt file and stores column value into ByteBuffer and then using getValue() method of it to get Double object. Now I have to print Double upto 12 significant digit after decimal. I found one answer from this from which is working fine but few cases
BigDecimal bd = new BigDecimal(dblColumnData.doubleValue());
System.out.println(String.format("%."+15+"G", bd));
Here 15 is given because there are 3 digits in integer part and there must be 12 significant digit after decimal.
Cases where it is not working is because BigDecimal created from Double. If I print Double then it contains more than 12 digits after decimal and it round correctly with same above approach.
Therefore I think if I can get similar format method for Double the it will solve my problem.
I'd do it somehow like this:
static double roundTo(double d, int digit) {
double exp = Math.pow(10, digit);
d *= exp;
d = Math.round(d);
return d / exp;
}
The following would round the number to 3 places after the comma and print it
public static void main(String[] args) {
System.out.println(roundTo(7.34343434, 3));
}
Will print "7.343"
This question already has answers here:
How can I truncate a double to only two decimal places in Java?
(18 answers)
Closed 8 years ago.
amount / 100 * 7 - I'am trying to get a percent from an amount, but the problem is that sometimes a get a number with to many digits after dot, how can I make it strictly return 2 digits after dot?
type is double
Use DecimalFormat API
double d = amount / 100 * 7;
DecimalFormat df = new DecimalFormat("#.##");
System.out.print(df.format(d));
"##" denotes the 2 digit precision
You can do this
double d = Math.round(amount * 7) / 100.0;
This will give you have value which has two decimal places. (for a modest range of values i.e. < 70e12)
If you just want to print two decimal places you can use
System.out.printf("%.2f%n", d);
This question already has answers here:
Why does the division of two integers return 0.0 in Java? [duplicate]
(6 answers)
Double value returns 0 [duplicate]
(3 answers)
Closed 9 years ago.
Why does Java return a 0 when I divide 10/60?
the code I tried is
double pay_per_minute = (10/60);
10 being the pay rate per hour and 60 being the minutes.
Because you're building an integer. When you store it in a double variable, it's already too late : it's 0.
Do
double pay_per_minute = (10.0/60);
If you have variables, cast them :
double pay_per_minute = ((double)pay_per_hour) / 60;
any primitive digit in java is treated as an integer, so when you do 10/60, it is integer division and due to precision loss it is giving 0
Here 10 and 60 takes as int values and then you get int dividing result it is 0 then you get answer as 0. use following way.
double a=10;
double b=60;
double div=a/b;
you need to type cast it first because by default numericals are considered as integers
double pay_per_minute = ((double)10/60);
System.out.println(pay_per_minute);
output 0.16666666666666666
double pay_per_minute = (10/60);
Here, you are dividing integer 10 by integer 60. So, this is like doing
int temp = 10/60;
double pay_per_minute = double(temp)
your temp will be 0 (since 10/60 is 0 when considered as integer division)
You need to do,
double pay_per_minute = (10.0/60);