• Post Reply Bookmark Topic Watch Topic
  • New Topic

Strange issue in converting String to Double  RSS feed

 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I'm trying to convert a string value "00000.001" to a Double and have used the usual converting methods like using constructor, valueOf and parseDouble methods but i'm getting output as .0010 but i want the output as .001 can anyone help me out with this...
 
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
abu bucker wrote:I'm trying to convert a string value "00000.001" to a Double and have used the usual converting methods like using constructor, valueOf and parseDouble methods but i'm getting output as .0010 but i want the output as .001 can anyone help me out with this...

Show us your code; it could be any number of things.

Winston
 
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This:
java.text.DecimalFormat


Or some combination of these:
String.format()
System.out.printf()
string formatting syntax
 
Ranch Hand
Posts: 3090
14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But it's most likely that your solution will be to use DecimalFormat and its format() method. Or one of the format() methods on String, PrintStream, or PrintWriter. The problem isn't in converting a String to a double - it's in displaying that double, which involves converting it back to a String.
 
Mike Simmons
Ranch Hand
Posts: 3090
14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Errr... as Jeff just said.
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mike Simmons wrote:The problem isn't in converting a String to a double - it's in displaying that double, which involves converting it back to a String.


Excellent point.

@abu bucker: It's important to realize that there is no distinction between a double holding 0.5 and a double holding 0.500000. They are the exact same thing. The double itself has no notion of "decimal places". That only comes into play when we display it.

 
abu bucker
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@Mike
Yes you got it rite.

I will try with DecimalFormat and check.
 
abu bucker
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The format method of DecimalFormat returns the value as a String but i want the value as Double.......

Check my code
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
abu bucker wrote:The format method of DecimalFormat returns the value as a String but i want the value as Double...

I think you're missing the point. Your code already has the value as a Double - in 'd'.

You seem to want System.out.println() to magically format your Double value exactly the way you want; and it WON'T.

Winston
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
abu bucker wrote:The format method of DecimalFormat returns the value as a String but i want the value as Double.......


No, what you want is for the String representation of that double to be formatted a certain way.

As I said previously: It's important to realize that there is no distinction between a double holding 0.5 and a double holding 0.500000. They are the exact same thing. The double itself has no notion of "decimal places". That only comes into play when we display it.

So, when you say:


you're talking about what you want from the String, not what you want from the double.
 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dear Abu, try this

double d = 00000.001;
String f = String.format("%.3f ", d);
===========================
output: .001
 
abu bucker
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jeff Verdegan wrote:

you're talking about what you want from the String, not what you want from the double.


I want the Double as .001 and not as .0010 after its converted from String to Double.

Although the above two values are same but this value is being stored in the DB and we are having some issues when the value is being stored as .0010 instead of .001.
 
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
abu bucker wrote:
Jeff Verdegan wrote:you're talking about what you want from the String, not what you want from the double.
I want the Double as .001 and not as .0010 after its converted from String to Double.

Although the above two values are same but this value is being stored in the DB and we are having some issues when the value is being stored as .0010 instead of .001.

What everyone is trying to tell you is that Double does not have an inherent format, so what you're asking for doesn't make sense. It has a value only. If you want to control what that value looks like when it's printed out....in other words, when it's converted to a string.... then you use DecimalFormat.
 
Marshal
Posts: 56600
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch anishanshu sharma.
 
abu bucker
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Winston GutkowskiI think you're missing the point. Your code already [i wrote:has[/i] the value as a Double - in 'd'.

You seem to want System.out.println() to magically format your Double value exactly the way you want; and it WON'T.

Winston


You are getting taking it in a wrong way.

The code that i have written is the simplified example of my actual code and the sysout was to only display the value.

All i want is that when i convert the String "00000.001" to a Double, the format should be 0.001 instead of 0.0010.
 
abu bucker
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Matthew Brown wrote:
abu bucker wrote:
Jeff Verdegan wrote:you're talking about what you want from the String, not what you want from the double.
I want the Double as .001 and not as .0010 after its converted from String to Double.

Although the above two values are same but this value is being stored in the DB and we are having some issues when the value is being stored as .0010 instead of .001.

What everyone is trying to tell you is that Double does not have an inherent format, so what you're asking for doesn't make sense. It has a value only. If you want to control what that value looks like when it's printed out....in other words, when it's converted to a string.... then you use DecimalFormat.


I know that Double does not have an inherent format but i was looking for a way to limit the number of digits after the decimal point for a Double.
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Matthew Brown wrote:
abu bucker wrote:I want the Double as .001 and not as .0010 after its converted from String to Double.

Although the above two values are same but this value is being stored in the DB and we are having some issues when the value is being stored as .0010 instead of .001.
What everyone is trying to tell you is that Double does not have an inherent format, so what you're asking for doesn't make sense. It has a value only. If you want to control what that value looks like when it's printed out....in other words, when it's converted to a string.... then you use DecimalFormat.

@abu: One alternative, if you really need to store a value with a specific number of decimal places, is to use BigDecimal rather than java.lang.Double. In fact, for fixed-decimal data such as currency, it is the recommended approach.

Winston
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
abu bucker wrote:
Jeff Verdegan wrote:

you're talking about what you want from the String, not what you want from the double.


I want the Double as .001 and not as .0010


Again (third time now): There is no such thing. There is no difference. The Double .001 IS EXACTLY THE SAME as the Double .0010. Not just mathematically, but in Java objects and primitives as well. You really do not want the double .001 instead of .0010. All you want is that it gets displayed a certain way. When you call println(d) it's not printing the raw double representation--it's converting it to a String first. That's the only way to display anything. So that means that what you are really talking about is not the double value, but the String representation of that double.

Everything you've said up to here says that you care about what gets displayed, not what gets stored in the double.

However...


Although the above two values are same but this value is being stored in the DB and we are having some issues when the value is being stored as .0010 instead of .001.


How is the column defined?

What is the code you're using to store it?

What is the code you're using to retrieve it?

What are these "some issues" that you're having?

Note 1: A double cannot actually store .001. There will be some small error because doubles are base-2 and .001 is not a sum of negative powers of 2.

Note 2: If what you really need is to "store 3 decimal digits of precision instead of 4", then the Java class for that is BigDecimal, and you would store it in the DB using PreparedStatement.setBigDecimal() and retrieve it with the appropriate ResultSet.getBigDecimal() call. You'll have to look into the docs for your DB or the driver to see how well it preserves BigD's attributes and values going back and forth.
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
abu bucker wrote:
Winston GutkowskiI think you're missing the point. Your code already [i wrote:has[/i] the value as a Double - in 'd'.

You seem to want System.out.println() to magically format your Double value exactly the way you want; and it WON'T.

Winston


You are getting taking it in a wrong way.


No, you are not understanding what we're telling you.

The code that i have written is the simplified example of my actual code and the sysout was to only display the value.

All i want is that when i convert the String "00000.001" to a Double, the format should be 0.001 instead of 0.0010.


And as we've told you several times: It's not possible. There's no difference. Double doesn't have a format.
 
anishanshu sharma
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dear abu
i have provided the solution for yourproblem have a look

double d = 00000.001;
String f = String.format("%.3f ", d);
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
abu bucker wrote:I know that Double does not have an inherent format but i was looking for a way to limit the number of digits after the decimal point for a Double.

That statement is a tautology: If Double doesn't have an inherent format, there can't possibly be a way of limiting the number of digits after the decimal point.

Winston
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
abu bucker wrote:
I know that Double does not have an inherent format but i was looking for a way to limit the number of digits after the decimal point for a Double.


If you know that double does not have an inherent format, then you know that it's not possible to "limit the number of digits."
 
Matthew Brown
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think you're looking for your error in the wrong place. You say the System.out.println() call was just an illustration, so what exactly are you doing with the Double? A Double just has a value - it's what you do with that value that counts.
 
Campbell Ritchie
Marshal
Posts: 56600
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
abu bucker wrote: . . . stored in the DB and we are having some issues when the value is being stored as .0010 instead of .001.
Please read this. You are now telling us something different from at first. If you are storing things in databases with terminal 0s, what format are they in? Are they stored as a DECIMAL(9, 3) or similar? If you want a number which actually stores a certain number of decimal places, you must give up doubles altogether. There is a sort of Number which stores a certain number of decimal places, but it is not called double. It is called BigDecimal. Clkcik on that last word and youwill find the details. If you pass a double like 0.1, yuou will suffer imprecision and inaccuracy, You might have to do something like this:You will find there is a correspondence between the SQL type DECIMAL and the Java™ type BigDecimal.
 
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Try this

 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!