3

How to get the double value that is only two digit after decimal point.

For example if the a = 190253.80846153846 then the result value should be like a = 190253.80

Try: I have try with this:

public static DecimalFormat twoDForm = new DecimalFormat("#0.00");

in code

a = Double.parseDouble(twoDForm.format(((a))));

But i got the value like 190253.81 instead of that i want 190253.80

So what should i have to change for it ??

RajaReddy PolamReddy
  • 22,428
  • 19
  • 115
  • 166
Shreyash Mahajan
  • 23,386
  • 35
  • 116
  • 188

7 Answers7

6

Because Math.round() Returns the closest int to the argument. The result is rounded to an integer by adding 1/2, taking the floor of the result, and casting the result to type int. Use Math.floor()

Example

 public static double roundMyData(double Rval, int numberOfDigitsAfterDecimal) {
                  double p = (float)Math.pow(10,numberOfDigitsAfterDecimal);
              Rval = Rval * p;
              double tmp = Math.floor(Rval);
              System.out.println("~~~~~~tmp~~~~~"+tmp);
              return (double)tmp/p;
              }

Complete Source code

class ZiggyTest2{


        public static void main(String[] args) {  
             double num = 190253.80846153846;
              double round = roundMyData(num,2);
              System.out.println("Rounded data: " + round);
              }

              public static double roundMyData(double Rval, int numberOfDigitsAfterDecimal) {
                  double p = (float)Math.pow(10,numberOfDigitsAfterDecimal);
              Rval = Rval * p;
              double tmp = Math.floor(Rval);
              System.out.println("~~~~~~tmp~~~~~"+tmp);
              return (double)tmp/p;
              }
            }
Sunil Kumar Sahoo
  • 53,011
  • 55
  • 178
  • 243
6

Try this,

make a object of BigDecimal

double a = 190253.80846153846;
BigDecimal bd = new BigDecimal(a);
BigDecimal res = bd.setScale(2, RoundingMode.DOWN);
System.out.println("" + res.toPlainString());
King RV
  • 3,760
  • 1
  • 15
  • 16
3

With no libraries:

a = (float) (((int)(a * 100)) / 100.0f);

or, for double:

a = (double) (((int)(a * 100)) / 100.0);
jcxavier
  • 2,232
  • 1
  • 15
  • 24
2

i think that is going to round of the value, check this

(float)Math.round(value * 100) / 100

from this link round of decimal number

or this example

Community
  • 1
  • 1
RajaReddy PolamReddy
  • 22,428
  • 19
  • 115
  • 166
2
Following code works for me.    
public static double round(double value, int places) {
            //here 2 means 2 places after decimal
            long factor = (long) Math.pow(10, 2);
            value = value * factor;
            long tmp = Math.round(value);
            return (double) tmp / factor;
        }
Shabbir Panjesha
  • 1,851
  • 5
  • 20
  • 29
1

Here is another way to write it that is similar to Shabbir's ^ but I think is easier to read. I actually wanted to round it; if it showed .349, I wanted to see .35 - but if you don't want that then you can use Math.floor in place of my Math.round:

public static double round(double time){
  time = Math.round(100*time);
  return time /= 100;
}

Here is my complete code: I am working on a program to find 3 random 1 min time intervals in a given time segment, for a research project im working on.

import java.lang.*;
import java.util.*;

class time{

public static void main (String[] args){

  int time = Integer.parseInt(args[0]);
  System.out.println("time is: "+time);
  //randomly select one eligible start time among many 
  //the time must not start at the end, a full min is needed from times given
  time = time - 1;
  double start1 = Math.random()*time;
  System.out.println(start1);
  start1 = round(start1);
  System.out.println(start1);

}

public static double round(double time){
  time = Math.round(100*time);
  return time /= 100;
}

}

to run it, with current output:

[bharthur@unix2 edu]$ java time 12
time is: 12
10.757832858914
10.76

[bharthur@unix2 edu]$ java time 12
time is: 12
0.043720864837211715
0.04
1
double dValue = 10.12345;
try{
    String str = Double.toString(dValue*100);`
    str = str.split("[.]")[0];
    dValue = Double.parseDouble(str)/100;
} catch (Exception e) {
    e.printStackTrace();
}
System.out.println(dValue);

Use this code. You can get the desired output you want.

mevada.yogesh
  • 1,118
  • 3
  • 12
  • 35