• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

format double

 
Ranch Hand
Posts: 251
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello,

Can any one please help me in formating a double like below?

Double s1 = Double.parseDouble(sAmount2)/sAmt *100;

I got the value for s1 as 73.177, I want to format s1 as 73.177%

How would I format the s1 as 73.177%?

Thanks
 
Ranch Hand
Posts: 1282
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
 
Ranch Hand
Posts: 81
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by lakshmi manepally:


Double s1 = Double.parseDouble(sAmount2)/sAmt *100;



Double.parseDouble(String) return double primitive.
How come you are getting Double wrapper object in return?
Is this syntax correct?

V
 
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Nicholas Jordan:


That would be NumberFormat. DecimalFormat is the most-used subclass.

Alternatively, you can use String.format in Java 5.0 and up:

That will use the locale specific decimal symbol; if you want a fixed locale use the following:
 
Rob Spoor
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Vishal Matere:


Double.parseDouble(String) return double primitive.
How come you are getting Double wrapper object in return?
Is this syntax correct?

V


That's called autoboxing. It allows you to switch between double and Double without any additional code. It's actually short hand:

is short hand for

This also shows a danger: if the Double is null, you'll get a NullPointerException:

This works for all primitives and their wrappers: (b/B)yte, (s/S)hort, int/Integer, (l/L)ong, (f/F)loat, (d/D)ouble, char/Character and (b/B)oolean.
 
phani kon
Ranch Hand
Posts: 251
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

The below code is working, But I need to convert into s into double.


String s = String.format("%.3f%%", s1);

I have done like below

Double st = Double.parseDouble(s);//Giving number format exceptionHow would I do that?
 
Nicholas Jordan
Ranch Hand
Posts: 1282
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Rob Prime:

That would be NumberFormat. DecimalFormat is the most-used subclass.



Is DecimalFormat consistent with String.format + does his %.3f%% format consistently with Class Double and DecimalFormat ( I assume the dot three controls number of places after the dot. )

Possibly poster needs to show surrounding code.

http://java.sun.com/j2se/1.5.0/docs/api/java/text/DecimalFormat.html
[ May 02, 2008: Message edited by: Nicholas Jordan ]
 
phani kon
Ranch Hand
Posts: 251
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How would I solve this problem?
 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Rob Prime has explained it already. Of course the String his code produces is 73.771% which is what you asked for originally. You can't parse that back to a Number because of the % character.
 
Rob Spoor
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Safest way is to cut off the % if present, then parse as usual:
 
phani kon
Ranch Hand
Posts: 251
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I don't want to parse %.I need to show the parcentage value in the double.

I hope you understand my problem
 
Campbell Ritchie
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, we do understand. We have given lots of useful hints; look at Rob Prime's last posting.
 
phani kon
Ranch Hand
Posts: 251
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I don't see any hint to format a double value with %.I need to format the value using double not String.
 
Rob Spoor
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Look at my first post in this thread. There are two good pieces of code that will do just that - take a double, and add a %. My last post showed how you can parse such a string back into a double.
 
phani kon
Ranch Hand
Posts: 251
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I understood what you are mentioning.For the below code



How would I add agian percentage sign to the double. I know I can append the percentage sign using string, but I want to do it in double.
 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I don't understand what you're trying to do - a double (or Double) is a number. It may signify a percentage, but there's no way to get to print with a percent sign. What's wrong with adding that character using string concatenation or DecimalFormat?
 
phani kon
Ranch Hand
Posts: 251
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here is my problem.

 
Ranch Hand
Posts: 1296
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

//Here the value of d should be in the format as 73.45%. How is it possible?


It's not possible. A double (or a Double) has no units. Period. Its just a number.
 
Nicholas Jordan
Ranch Hand
Posts: 1282
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Class Double is not the same as primitive double. I suggest storing class Double(s) in your array. To append the '%' sign, use the StringBuffer class, from which you can derive a String when you call the method thus:


This is closer than what you had, may need work.

{ message edit, I read the other responses, ask the poster what this does - I have to run an errand.
[ May 05, 2008: Message edited by: Nicholas Jordan ]
 
Campbell Ritchie
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Double trouble = new Double((int) '%');

Why do you need the cast?
 
Campbell Ritchie
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
And Lakshmi Manepally, take no notice about Double trouble; we are having you on. But it is a useful lesson to see whether that bit of code will run, with or without the cast.

[edit]Minor spelling correction[/edit]
[ May 05, 2008: Message edited by: Campbell Ritchie ]
 
Nicholas Jordan
Ranch Hand
Posts: 1282
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Campbell Ritchie:
(...snip...))take no notice about Double trouble



I do not undersand why not notice, a file-scope class could be built to do the formatting OP places bold tags around Here the value of d should be in the format as 73.45%. How is it possible?. I suppose class DecimalFormat is where you guys want to go and as long as OP gets useable code I don't see any correctness issues.

[Campbell Ritchie:]  we are having you on. You mean 'helping' you on? - not as an issue, just so I can grasp your statement.

[Campbell Ritchie:]  But it is a useful lesson to see whether that bit of code will run, with or without the cast.
I did not want to stop and explain casts, nor how a numeric could come from a character - I intended, by doing the cast, to draw attention to the fact that the formatting character which OP wants in displayed representation has nothing functional to do with the value of the Double. While I ran my errand it occured to me to suggest to OP the inclusion of a default access class that does what is needed.

At that point, something like DecimalFormat is within scope of skills.

{neutral smiley}
[ May 05, 2008: Message edited by: Nicholas Jordan ]
 
phani kon
Ranch Hand
Posts: 251
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No it is not working.
 
Nicholas Jordan
Ranch Hand
Posts: 1282
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by lakshmi manepally:
No it is not working.



What is not working?...
 
Campbell Ritchie
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Agree with Nicholas Jordan. What is not working?

You started asking for a means of formatiing a double number into a String like 74.337%, so we told you.
Then you asked how you could get it back to a double, minus the %, so we told you.
Then you said you needed it as a double, or a Double, with the % character reattached, so we told you.
Then you said you wanted a double with % attached for a dataset, and we told you why you cna't pass the double with % attached.
Then we demonstrated that you can turn the % char into a number.

As Nicholas Jordan said, what is not working?
 
phani kon
Ranch Hand
Posts: 251
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here is my code.


In the previous code, I don't understand how can I cast (int) %.


s1 = 73.45;
s = String.format("%.3f%%", s1);

with the above code I got s as 73.45%.

d = Double.valueOf(s);//Here Iam getting the number format exception because of the %.


if (s.endsWith("%"))
{ s = s.substring(0, s.length() - 1);}


So I have done like bleow

Double doubleObject = Double.valueOf(s);
StringBuffer sb = new StringBuffer(doubleObject.toString());

sb.append('%');

Now I don't understand the bleow code,

dataset.setValue(sb.toString(), int position);//here why are you puuting int instead of double

If you don't mind can you please explain

Thanks a lot for your help.
 
Campbell Ritchie
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by lakshmi manepally:
. . .

dataset.setValue(sb.toString(), int position);//here why are you puuting int instead of double

Whatever your dataset is, you are passing it two things:
  • A String which will be in the format "73.771%" or similar,
  • an int, which is called position, so 1 for 1st value, 2 for 2nd value, etc.
  • You are not actually passing a double value to it.

    You have gone all round the Moon to get at the Sun; you appear to have turned "73.771" into 73.771 then back to "73.771" and added a %. If you really only needed to get "73.771" to "73.771%" you could have done it like this:You will have to find who gave you whatever "dataset" is, probably some sort of database entry object or similar, and get the details of how it is supposed to work.
     
    phani kon
    Ranch Hand
    Posts: 251
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    But with the below code
    s += "%";

    How would I pass the value s into the below method for the double?

    dataset.setValue(String s, double s);

    Any suggestions please...


    Thanks
     
    Campbell Ritchie
    Marshal
    Posts: 79177
    377
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Originally posted by lakshmi manepally:
    But with the below code
    s += "%";

    How would I pass the value s into the below method for the double?

    dataset.setValue(String s, double s);

    Any suggestions please...


    Thanks

    Not at all. Actually the code you have quoted won't compile; there are two method parameters called "s".

    You keep changing your requirements; last time you said setValue took the parameters (String, int).

    We have already told you. If your String is of the correct format, eg "73.771" then you can pass it to methods of the Double class like valueOf(String); you can add the % with the += operator and pass the String now "73.771%" to the setValue() method. Rob Prime has explained it to you already, and told you how to get rid of the % if you have to.
     
    Nicholas Jordan
    Ranch Hand
    Posts: 1282
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    lakshmi: What you are trying to do here is have the '%' be part of the double, that will not happen. You can 'make magic' by writing a class in the file ( or nested class ) that has both a double and the '%' character. Then, by writing a get method, your code can append the '%' in the .toString() method of that class. Strings are not numbers, numbers do not have a '%' and percentages are - by definition - a ratio or shifting of the decimal point. You should encapsulate that in a handler class you write yourself. Eventually you will find a library function that suits the need.

    You will never force a '%' into a double. Ease up, think about it. These posters are busy.
     
    Greenhorn
    Posts: 14
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Could you do something like this in the dateset class?

     
    With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
    reply
      Bookmark Topic Watch Topic
    • New Topic