Solved

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

Posted on 2003-03-04
Medium Priority
248 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
[X]
###### Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

• Help others & share knowledge
• Earn cash & points

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

In this post we will learn different types of Android Layout and some basics of an Android App.
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arrâ€¦
This video teaches viewers about errors in exception handling.
###### Suggested Courses
Course of the Month13 days, 11 hours left to enroll