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);
Related
This question already has answers here:
How to round a number to n decimal places in Java
(39 answers)
Closed 5 years ago.
I have the following variables initialized to the user inputs,
int completedPasses = reader1.nextInt();
int attemptedPasses = reader2.nextInt();
double completionRatio = (250 / 3) * ((completedPasses / attemptedPasses) - 0.3);
How do I round the computation above to a double of one decimal place?
Use DecimalFormat:
DecimalFormat df = new DecimalFormat("#.#");
df.format(completionRatio);
Or Math.round:
Math.round(completionRatio);
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
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:
Round number to only first decimal place
(3 answers)
Closed 8 years ago.
I wish I could understand how it is possible that is not rounded the decimal number obtained from the following code.
File path2 = Environment.getDataDirectory();
StatFs stat2 = new StatFs(path.getPath());
long blockSize2 = stat.getBlockSize();
long availableBlocks = stat.getAvailableBlocks();
double result = availableBlocks * blockSize;
free = (Preference)this.findPreference("free_mem");
free.setSummary(Double.toString(result)+" GB");
In a code similar to this use this instruction and works
result = Math.round(result * 10) / 10d;
Why not work here and I still see a number with many decimal places?
If I understood your question right you need NumberFormat here:
NumberFormat nf = NumberFormat.getInstance();
nf.setMaximumFractionDigits(1);
nf.format(result);
This produces a number with 1 decimal places.
So if result is 6.6789 it will produce 6.7.
Related: Round number to only first decimal place
Just a note:
If you do this:
Math.round(result * 10) / 10d;
you basically say:
Multiply result with 10
Round the result
Then divide with ten.
When you got rid of the decimals at step 2. you got another bunch of decimals after the division.
This question already has answers here:
How to round a number to n decimal places in Java
(39 answers)
Round a double to 2 decimal places [duplicate]
(13 answers)
Closed 9 years ago.
I am trying to Format my double value to exact 2 decimal places and it seems to working fine, here is the code i am trying
final NumberFormat df = DecimalFormat.getInstance();
df.setMinimumFractionDigits(2);
df.setMaximumFractionDigits(2);
df.setRoundingMode(RoundingMode.DOWN);
df.format(value)
Till now everything is working, but i need to return double value as it is being used for other calculations and i tried
Double.parseDouble(df.format(value))
with big decimal like
BigDecimal price = new BigDecimal(df.format(number));
but it is not working as expected
like 18.50 is being converted as 18.5
Though this is not an issue with calculations but i need to show amount on the UI where i have to show exactly up to 2 decimal places.
Is there any was i can handle it in java class or i have to take care in JSP with JSTL
This is what BigDecimal is made for!
BigDecimal number = new BigDecimal(123.456);
// set 2 fraction digits
// Note that setScale() does not change the original,
// but returns a new BigDecimal.
number = number.setScale(2, RoundingMode.DOWN);
// get string representation
String text = number.toPlainString();
// get double value
double dbl = number.doubleValue();
And use BigDecimal for other calculations as well if you can.