[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Java

Posted on 2009-02-20
28
Medium Priority
?
235 Views
Last Modified: 2012-05-06
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
Comment
Question by:rachelee
  • 9
  • 8
  • 7
  • +1
28 Comments
 
LVL 26

Expert Comment

by:ksivananth
ID: 23692945
use BigDecimal
0
 
LVL 26

Expert Comment

by:ksivananth
ID: 23692951
new BigDecimal( value ).setScale( 4, BigDecimal.ROUND_HALF_UP ) ;
0
 
LVL 86

Expert Comment

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

Open in new window

0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 26

Assisted Solution

by:ksivananth
ksivananth earned 600 total points
ID: 23692966
to get the double value back,

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

Author Comment

by:rachelee
ID: 23693298
If my pattern is like String ("#0.####") thennn


0
 
LVL 86

Expert Comment

by:CEHJ
ID: 23693347
>>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

by:rachelee
ID: 23693508
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
 
LVL 26

Expert Comment

by:ksivananth
ID: 23693525
>>How shall I get rrounded value 2,1235 from 2,12345?

which locale?
0
 

Author Comment

by:rachelee
ID: 23693559
FormatString shud return me that value if I give any string as patter2
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 23693567
>>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

by:CEHJ
ID: 23693597
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
 

Author Comment

by:rachelee
ID: 23693875
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

by:CEHJ
CEHJ earned 200 total points
ID: 23694104
System.out.printf("Outout is %.4f\n", 1.12345);
0
 
LVL 92

Expert Comment

by:objects
ID: 23695622
0
 

Author Comment

by:rachelee
ID: 23709144
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
 
LVL 92

Accepted Solution

by:
objects earned 1200 total points
ID: 23709172
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

by:objects
ID: 23709179
you can also use the BigDecimal toString() method

String s = bd.toString();

0
 
LVL 86

Expert Comment

by:CEHJ
ID: 23709836
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
 

Author Comment

by:rachelee
ID: 23710126
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

by:objects
objects earned 1200 total points
ID: 23710143
change your format to:

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


0
 

Author Comment

by:rachelee
ID: 23712037
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

by:rachelee
ID: 23712077
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

by:CEHJ
ID: 23713695
>>but does not serve the purpose:-(

Why not?
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 23713882
>>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

by:objects
ID: 23717388
> but not for 1.1230 from 1.2291

shouldn't that be 1.2295?

0
 
LVL 92

Expert Comment

by:objects
ID: 23717399
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

by:objects
ID: 23717428
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

by:rachelee
ID: 23720403
Dear Object ,ksivananth and CEHJ,

 SOLVED!

thnx a lot..

Much regards,
Rachel
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
Suggested Courses
Course of the Month20 days, 2 hours left to enroll

872 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question