rachelee
asked on
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
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
use BigDecimal
new BigDecimal( value ).setScale( 4, BigDecimal.ROUND_HALF_UP ) ;
Rounding would take place on display, so:
String displayVal = String.format("%.4f", yourDouble);
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
If my pattern is like String ("#0.####") thennn
>>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
Sorry - don't understand the question. If you mean what if it's less than 1, then that's not relevant
ASKER
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
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;
>>How shall I get rrounded value 2,1235 from 2,12345?
which locale?
which locale?
ASKER
FormatString shud return me that value if I give any string as patter2
>>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?
Formatting involves starting with a number and ending with a String - you seem to be starting with a String. Is that right?
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;
}
ASKER
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.
}
}
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.
}
}
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Dear Objects,
I know what do u mean..but problem occurs in that case to..try my code:-)
thnx
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);
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
you can also use the BigDecimal toString() method
String s = bd.toString();
String s = bd.toString();
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);
ASKER
Dear Object
Ya it did work partially, ow it does not give right output for value 1.22296
it shud be 1.2230:-)
Why?
Ya it did work partially, ow it does not give right output for value 1.22296
it shud be 1.2230:-)
Why?
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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..
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..
ASKER
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:-(
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:-(
>>but does not serve the purpose:-(
Why not?
Why not?
>>but not for 1.1230 from 1.2291
How would that occur - the first number is quite a bit lower than the second?...
How would that occur - the first number is quite a bit lower than the second?...
> but not for 1.1230 from 1.2291
shouldn't that be 1.2295?
shouldn't that be 1.2295?
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();
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(
.setScale(4, BigDecimal.ROUND_HALF_UP);
return df.format(bd.doubleValue()
// or return bd.toString();
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.
ASKER
Dear Object ,ksivananth and CEHJ,
SOLVED!
thnx a lot..
Much regards,
Rachel
SOLVED!
thnx a lot..
Much regards,
Rachel