Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How do I convert a DOUBLE datatype to a two decimal number

Status
Not open for further replies.

JavaP100

Programmer
May 18, 2006
6
US
Hi All,

I have a method that retrieves data from the database. The "Price" column (one of the column it returns) returns a DOUBLE data type. Which means the number could have three numbers AFTER the decimal point. I only want the UI to show a number of a max of TWO decimal points,

Is there some type of NumberFormat API i can use to truncate the third number or "round" up to two decimal points? I read through the NumberFormat Java API and it wasnt clear on how to do this.

Thx
 
Take a look at the DecimalFormat class.

Hope this helps.

"If you say you can, or you say you can't, you're right!"
-- Henry Ford
 
A double can have much more than three decimals.

Anyway, if you want to do it the simple way:

Code:
double myThreeDecimalsDouble = 1.234;
double myTwoDecimalsDouble = Math.round(myThreeDecimalsDouble*100)/(double)100.00;

Cheers,
Dian
 
or :

Code:
double d = 111.123456;

double d2 = (int)((d * 100)) / 100.0;

--------------------------------------------------
Free Java/J2EE Database Connection Pooling Software
 
It will, but the OP says a trunc was fine

--------------------------------------------------
Free Java/J2EE Database Connection Pooling Software
 
But what he actually wants is to round it, trust me. Maybe he still doesn't know it, but that's the truth ;)

Cheers,
Dian
 
Thx for the input. I tried this code sugguested above.
-----------------------------------------------------
double myThreeDecimalsDouble = 1.234;
double myTwoDecimalsDouble = Math.round(myThreeDecimalsDouble*100)/(double)100.00;
----------------------------------------------------

it works fine. But problem is that if the Double is 1234.702 (for example) it still displays as 1234.7. i.e it drops the zero. I would want it to still display as 1234.70

Any ideas?
 
Code:
double d = 111.70321321;
DecimalFormat df = new DecimalFormat("###0.00");
System.out.println(df.format(d));

--------------------------------------------------
Free Java/J2EE Database Connection Pooling Software
 
Hello,

Thx again.

The problem with the code below is that format() returns only a String. I need the value in a double data type. When i try to cast into Double(using Double.parseDouble().

It throws no error but it trunates the zero i.e 1234.70 becomes 1234.7

Is there a work around?

-----------------------------------------------
double d = 111.70321321;
DecimalFormat df = new DecimalFormat("###0.00");
System.out.println(df.format(d));

--------------------------------------------
 
Humm.

A double is a value, so 1234.7 is the same as 1234.70.

If you want to display it, use the String. If you want to store it, use double. If you just want to store it with a concrete precision, don't worry about trailing zeros

Cheers,
Dian
 
My answer is the same as Dian's - if you want a double - then use a double - if you want a string to display then use my code !


--------------------------------------------------
Free Java/J2EE Database Connection Pooling Software
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top