?
Solved

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

Posted on 2003-03-04
9
Medium Priority
?
248 Views
Last Modified: 2010-03-31
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!!!!
0
Comment
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
  • Learn & ask questions
9 Comments
 
LVL 3

Expert Comment

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

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

by:objects
ID: 8069748
If you need to store fixed decimal places then use BigDecimal class.
0
Get real performance insights from real users

Key features:
- Total Pages Views and Load times
- Top Pages Viewed and Load Times
- Real Time Site Page Build Performance
- Users’ Browser and Platform Performance
- Geographic User Breakdown
- And more

 

Accepted Solution

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

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

Author Comment

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

by:objects
ID: 8069804
> what datatype should i use?

store it as a string.
0
 
LVL 1

Expert Comment

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

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

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

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

800 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