Solved

# Java

Posted on 2009-02-20
225 Views
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
0
Question by:rachelee

LVL 26

Expert Comment

use BigDecimal
0

LVL 26

Expert Comment

new BigDecimal( value ).setScale( 4, BigDecimal.ROUND_HALF_UP ) ;
0

LVL 86

Expert Comment

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

LVL 26

Assisted Solution

to get the double value back,

new BigDecimal( value ).setScale( 4, BigDecimal.ROUND_HALF_UP ).doubleValue() ;
0

Author Comment

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

0

LVL 86

Expert Comment

>>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

Author Comment

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;
``````
0

LVL 26

Expert Comment

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

which locale?
0

Author Comment

FormatString shud return me that value if I give any string as patter2
0

LVL 86

Expert Comment

>>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

LVL 86

Expert Comment

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;

}
``````
0

Author Comment

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

LVL 86

Assisted Solution

System.out.printf("Outout is %.4f\n", 1.12345);
0

LVL 92

Expert Comment

0

Author Comment

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);
``````
0

LVL 92

Accepted Solution

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

LVL 92

Expert Comment

you can also use the BigDecimal toString() method

String s = bd.toString();

0

LVL 86

Expert Comment

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);
``````
0

Author Comment

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

LVL 92

Assisted Solution

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

0

Author Comment

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

Author Comment

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

LVL 86

Expert Comment

>>but does not serve the purpose:-(

Why not?
0

LVL 86

Expert Comment

>>but not for 1.1230 from 1.2291

How would that occur - the first number is quite a bit lower than the second?...
0

LVL 92

Expert Comment

> but not for 1.1230 from 1.2291

shouldn't that be 1.2295?

0

LVL 92

Expert Comment

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

LVL 92

Expert Comment

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

Author Comment

Dear Object ,ksivananth and CEHJ,

SOLVED!

thnx a lot..

Much regards,
Rachel
0

## Featured Post

### Suggested Solutions

For customizing the look of your lightweight component and making it look opaque like it was made of plastic. Â This tip assumes your component to be of rectangular shape and completely opaque. Â  (CODE)
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used byâ€¦
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define â€¦
The viewer will learn how to implement Singleton Design Pattern in Java.