wood burning stoves 2.0*
The moose likes Java in General and the fly likes How to get two digit decimal point in double Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "How to get two digit decimal point in double" Watch "How to get two digit decimal point in double" New topic
Author

How to get two digit decimal point in double

sri ramvaithiyanathan
Ranch Hand

Joined: Nov 20, 2010
Posts: 109
Hi,

I used numberformat to get two digit decimal. (Note its return type is string)
In database I need to save this value as double.

So I converted string to double. while converting I am getting decimal value as one digit instead of two.
Is there any way to get two digit decimal point in double.


Output:


P.S: I am facing this problem only when the input is of type x.00

Regards,
Sriram.V


For java examples,ebooks,interview questions,visit this blog
http://periodicupdates.blogspot.com/
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18508
    
  40

sri ramvaithiyanathan wrote:Hi,

I used numberformat to get two digit decimal. (Note its return type is string)
In database I need to save this value as double.

So I converted string to double. while converting I am getting decimal value as one digit instead of two.
Is there any way to get two digit decimal point in double.


Output:


P.S: I am facing this problem only when the input is of type x.00

Regards,
Sriram.V



There is no such a thing as a format regarding double values -- there is no one digit or two digit. The reason it is showing up as one digit is that the println() is converting it back to a string prior to printing it -- so if you want two digits, convert it back to a string (yourself instead of having the core library do it) and print that string instead.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Randall Twede
Ranch Hand

Joined: Oct 21, 2000
Posts: 4340
    
    2

or use BigDecimal instead


SCJP
Visit my download page
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 37940
    
  22
If you need to retain the number of places after the decimal point, and you are dealing with a number which you might do arithmetic on, consider aBigDecimal. Beware:new BigDecimal(1.23) does not contain 1.23. It contains something which differs slightly from 1.23. New BigDecimal("1.23") contains exactly 1.23. You can set the scale or round a BigDecimal.
If you are using BigDecimal in Java™, you will probably want a DECIMAL(??, 2) as your type in the database.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 37940
    
  22
Have you been taking lessons from Rob Spoor, Randall Twede?
Randall Twede
Ranch Hand

Joined: Oct 21, 2000
Posts: 4340
    
    2

lol Campbell. no, i have just been using BigDecimal and BigInteger(even more) lately
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19651
    
  18

Campbell meant you beating him in your timing. I tend to do that sometimes, especially to Campbell


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 37940
    
  22
You are doing well; 45 seconds’ difference, but Rob usually manages 30 seconds
sri ramvaithiyanathan
Ranch Hand

Joined: Nov 20, 2010
Posts: 109
Thanks to all.

Regards,
Sriram.V
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 37940
    
  22
You’re welcome
 
 
subject: How to get two digit decimal point in double
 
Similar Threads
need a double returned with 2 decimal points
Amount in Words
How to avoid exponential in STRING to DOUBLE conversion
Decimals not converting to correct fractions
null value in wrapper