Solved

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

Posted on 2003-03-04
Medium Priority
250 Views
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

0
Question by:ferric77

LVL 3

Expert Comment

ID: 8069495
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
0

LVL 26

Expert Comment

ID: 8069498
try the following,

NumberFormat formatter ;

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

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

Siva
0

LVL 92

Expert Comment

ID: 8069748
If you need to store fixed decimal places then use BigDecimal class.
0

Accepted Solution

anil001 earned 200 total points
ID: 8069775
if you not bothered about the currency but only format to two precision point then use the following.

import java.text.NumberFormat;

public class test02
{
public static void main(String[] args)
{
double a = 1;
double b = 3;
double d = a/b;
NumberFormat nf = NumberFormat.getInstance();
nf.setMaximumFractionDigits(2);
System.out.println("nomal output " + d);
System.out.println("Formatted Outupt " + nf.format(d));
}
}
output
Nomal output 0.3333333333333333
Formatted Outupt 0.33
0

Author Comment

ID: 8069778
But how do i store the value into DB2, if i use BigDecimal? n what datatype should i use?
0

Author Comment

ID: 8069792
But how do i store the value into DB2, if i use BigDecimal? n what datatype should i use?
0

LVL 92

Expert Comment

ID: 8069804
> what datatype should i use?

store it as a string.
0

LVL 1

Expert Comment

ID: 8071018
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
0

Expert Comment

ID: 9058921
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.
0

Featured Post

Question has a verified solution.

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

Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectioâ€¦
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 how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Downâ€¦
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:
Suggested Courses
Course of the Month12 days, 17 hours left to enroll

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

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