Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to get two digit decimal point in double

 
sri ramvaithiyanathan
Ranch Hand
Posts: 109
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Henry Wong
author
Marshal
Pie
Posts: 21114
78
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Randall Twede
Ranch Hand
Posts: 4385
3
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
or use BigDecimal instead
 
Campbell Ritchie
Sheriff
Posts: 48910
58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 48910
58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Have you been taking lessons from Rob Spoor, Randall Twede?
 
Randall Twede
Ranch Hand
Posts: 4385
3
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
lol Campbell. no, i have just been using BigDecimal and BigInteger(even more) lately
 
Rob Spoor
Sheriff
Pie
Posts: 20527
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell meant you beating him in your timing. I tend to do that sometimes, especially to Campbell
 
Campbell Ritchie
Sheriff
Posts: 48910
58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are doing well; 45 seconds’ difference, but Rob usually manages 30 seconds
 
sri ramvaithiyanathan
Ranch Hand
Posts: 109
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks to all.

Regards,
Sriram.V
 
Campbell Ritchie
Sheriff
Posts: 48910
58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You’re welcome
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic