Java

Hello,

Can somebody tell me how can I use math round method to get double value changed into some perticular value.
Ex. if value 1.12345
I should get rounded to the fourth digit like value 1.1235

thnxx
racheleeAsked:
Who is Participating?
 
objectsConnect With a Mentor Commented:
use what ksivananth suggested earlier



        double d = 1.12345; //if I input this special string it does not work otherwise it does work well.
        BigDecimal bd = new BigDecimal(d);
        bd = bd.setScale(4, BigDecimal.ROUND_HALF_UP);

        NumberFormat numberFormat =  new DecimalFormat        ("#0.####");
       
        String s = numberFormat .format(bd.doubleValue());
       
        System.out.println( s);  

0
 
ksivananthCommented:
use BigDecimal
0
 
ksivananthCommented:
new BigDecimal( value ).setScale( 4, BigDecimal.ROUND_HALF_UP ) ;
0
Cloud Class® Course: Python 3 Fundamentals

This course will teach participants about installing and configuring Python, syntax, importing, statements, types, strings, booleans, files, lists, tuples, comprehensions, functions, and classes.

 
CEHJCommented:
Rounding would take place on display, so:
String displayVal = String.format("%.4f", yourDouble);

Open in new window

0
 
ksivananthConnect With a Mentor Commented:
to get the double value back,

new BigDecimal( value ).setScale( 4, BigDecimal.ROUND_HALF_UP ).doubleValue() ;
0
 
racheleeAuthor Commented:
If my pattern is like String ("#0.####") thennn


0
 
CEHJCommented:
>>If my pattern is like String ("#0.####") thennn

Sorry - don't understand the question. If you mean what if it's less than 1, then that's not relevant
0
 
racheleeAuthor Commented:
CHEJ,

I'm sorry .
In this below case, what would you suggest me
How shall I get rrounded value 2,1235 from 2,12345?

thnx

static {
    pattern1 = new String("#0.###");
    pattern2 = new String("#0.####");
  }
 
 protected DecimalFormat deci;
 
 
  protected SomeMthod() {
   
    deci.applyPattern(pattern2);
  }
 
public String format(String myString) {
    String formatString= null;
 
    if ((myString!= null) && !myString.equals("null") && (myString.length() > 0)) {
        
      formatString= deci.format(Double.parseDouble);
    }
 
    return FormatString;

Open in new window

0
 
ksivananthCommented:
>>How shall I get rrounded value 2,1235 from 2,12345?

which locale?
0
 
racheleeAuthor Commented:
FormatString shud return me that value if I give any string as patter2
0
 
CEHJCommented:
>>public String format(String myString) {

Formatting involves starting with a number and ending with a String - you seem to be starting with a String. Is that right?
0
 
CEHJCommented:
If it is right then i'd do:
    public String format(String myString) {
        final String DECIMAL_4DP = "%.4f";
        String result = null;
 
        if ((myString!= null) && !myString.equals("null") && ((myString= myString.trim()).length() > 0)) {
            result = String.format(DECIMAL_4DP, Double.parseDouble(myString));
        }
        return result;
    }

Open in new window

0
 
racheleeAuthor Commented:
Ummmm...I cud not understand it CEHJ,

May be something like below:-))

public class math {

 
    public static void main(String[] args) {


       //String value= String.format("%.4f", 1.12345);
        //System.out.print("Value is  " + value);

How will I be able to use below code to get the result like I got from above two lines?
        String displayVal1 = String.format("#0.####", 1.12345);
        System.out.print("Outout is " + displayVal1);
       
        DecimalFormat df = DecimalFormat.getInstance();
        df.
    }

   
}


0
 
CEHJConnect With a Mentor Commented:
System.out.printf("Outout is %.4f\n", 1.12345);
0
 
racheleeAuthor Commented:
Dear Objects,

I know what do u mean..but problem occurs in that case to..try my code:-)
thnx


		
		
		double d = 1.12345; //if I input this special string it does not work otherwise it does work well. 
		
		NumberFormat numberFormat =  new DecimalFormat        ("#0.####"); 
		
		String s = numberFormat .format(d); 
		
		System.out.println( s);  

Open in new window

0
 
objectsCommented:
you can also use the BigDecimal toString() method

String s = bd.toString();

0
 
CEHJCommented:
Not sure why you want to use NumberFormat - it's kind of obsolete, but if you do, the following works fine:
        double d = 1.12345; 
        NumberFormat numberFormat =  NumberFormat.getInstance();
        numberFormat.setMaximumFractionDigits(4);
        String s = numberFormat .format(d);
        System.out.println( s);

Open in new window

0
 
racheleeAuthor Commented:
Dear Object
Ya it did work partially, ow it does not give right output for value 1.22296
it shud be 1.2230:-)
Why?

   
     

           

0
 
objectsConnect With a Mentor Commented:
change your format to:

        NumberFormat numberFormat =  new DecimalFormat        ("#0.0000");


0
 
racheleeAuthor Commented:
No dear it does not help:-(

thnx though!

  ("#0.0000");  this works for getting 1.1235 from input 1.12345
but not for 1.1230 from 1.2291

Please check it on ur comp.

Actually I want that the value should be closed on 4th place after point and shud round off as well..at 5 or 0..

 
0
 
racheleeAuthor Commented:
CEHJ,

thnx yeah!
I tried man,


the lines u suggested does work partially...it round off on 4th place after point but does not serve the purpose:-(


0
 
CEHJCommented:
>>but does not serve the purpose:-(

Why not?
0
 
CEHJCommented:
>>but not for 1.1230 from 1.2291

How would that occur - the first number is quite a bit lower than the second?...
0
 
objectsCommented:
> but not for 1.1230 from 1.2291

shouldn't that be 1.2295?

0
 
objectsCommented:
assuming that is the case try something like this:

DecimalFormat df = new DecimalFormat("#0.0000");
BigDecimal bd = new BigDecimal((d * 2.0) + 0.0005)
   .setScale(3, BigDecimal.ROUND_HALF_UP);
bd = new BigDecimal(bd.doubleValue()/2.0)
   .setScale(4, BigDecimal.ROUND_HALF_UP);
return df.format(bd.doubleValue());
// or return bd.toString();

0
 
objectsCommented:
on a side note, we try and encourage our students to first write a unit test. In situations like this a unit test is invaluable and makes your like a lot easier. The small amount of time involved setting up the unit test is quickly recovered while developing the required algorithm.

0
 
racheleeAuthor Commented:
Dear Object ,ksivananth and CEHJ,

 SOLVED!

thnx a lot..

Much regards,
Rachel
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.