Link to home
Start Free TrialLog in
Avatar of ferric77
ferric77

asked on

How to convert Double to 2 precision???????????

i have

double d;
double a = 1;
double b = 3;

d = 1/3;
the result is 0.333333333333....

How do i get 0.30

becouse the value is mean for currency. so i cant have thing like $0.33333
how can i get $0.30

PLEASE HELP ME!!!!
Avatar of allahabad
allahabad

double d;
d= 1/3;

NumberFormat numFormat = NumberFormat.getCurrencyInstance() ; // get the currency symbol into default locale

String value = numFormat.format(d); // convert into say $0.33
try the following,

      NumberFormat formatter ;

      formatter = NumberFormat.getCurrencyInstance(Locale.US) ;
      String strAmount = formatter.format( amount ) ;

System.out.println( " strAmount " + strAmount ) ;

Siva
Avatar of Mick Barry
If you need to store fixed decimal places then use BigDecimal class.
ASKER CERTIFIED SOLUTION
Avatar of anil001
anil001

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of ferric77

ASKER

But how do i store the value into DB2, if i use BigDecimal? n what datatype should i use?
But how do i store the value into DB2, if i use BigDecimal? n what datatype should i use?
> what datatype should i use?

store it as a string.
This question was asked before, and I still think (as I thought then) that the best way is using the primitives in java like this:

double d1 = 1;
double d2 = 3;
double d3 = d1/d2;
d3 = ((int)(d3 * 10)) / 10.0;

- now, d3 is 0.3
--shaji
ferric77:
This old question needs to be finalized -- accept an answer, split points, or get a refund.  For information on your options, please click here-> http:/help/closing.jsp#1 
EXPERTS:
Post your closing recommendations!  No comment means you don't care.