aspose file tools*
The moose likes Java in General and the fly likes format double Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "format double" Watch "format double" New topic
Author

format double

phani kon
Ranch Hand

Joined: Apr 06, 2005
Posts: 251
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
Nicholas Jordan
Ranch Hand

Joined: Sep 17, 2006
Posts: 1282
Vishal Matere
Ranch Hand

Joined: Jan 22, 2008
Posts: 81
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


SCJP <br />SCWCD <br />SCBCD <br />SCEA-1
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19541
    
  16

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:


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19541
    
  16

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

Joined: Apr 06, 2005
Posts: 251
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

Joined: Sep 17, 2006
Posts: 1282
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

Joined: Apr 06, 2005
Posts: 251
How would I solve this problem?
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36478
    
  16
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

Joined: Oct 27, 2005
Posts: 19541
    
  16

Safest way is to cut off the % if present, then parse as usual:
phani kon
Ranch Hand

Joined: Apr 06, 2005
Posts: 251
I don't want to parse %.I need to show the parcentage value in the double.

I hope you understand my problem
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36478
    
  16
Yes, we do understand. We have given lots of useful hints; look at Rob Prime's last posting.
phani kon
Ranch Hand

Joined: Apr 06, 2005
Posts: 251
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

Joined: Oct 27, 2005
Posts: 19541
    
  16

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

Joined: Apr 06, 2005
Posts: 251
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.
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 39547
    
  27
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?


Ping & DNS - updated with new look and Ping home screen widget
phani kon
Ranch Hand

Joined: Apr 06, 2005
Posts: 251
Here is my problem.

Garrett Rowe
Ranch Hand

Joined: Jan 17, 2006
Posts: 1296
//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.


Some problems are so complex that you have to be highly intelligent and well informed just to be undecided about them. - Laurence J. Peter
Nicholas Jordan
Ranch Hand

Joined: Sep 17, 2006
Posts: 1282
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
Sheriff

Joined: Oct 13, 2005
Posts: 36478
    
  16
Double trouble = new Double((int) '%');

Why do you need the cast?
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36478
    
  16
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

Joined: Sep 17, 2006
Posts: 1282
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

Joined: Apr 06, 2005
Posts: 251
No it is not working.
Nicholas Jordan
Ranch Hand

Joined: Sep 17, 2006
Posts: 1282
Originally posted by lakshmi manepally:
No it is not working.


What is not working?...
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36478
    
  16
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

Joined: Apr 06, 2005
Posts: 251
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
Sheriff

Joined: Oct 13, 2005
Posts: 36478
    
  16
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

    Joined: Apr 06, 2005
    Posts: 251
    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
    Sheriff

    Joined: Oct 13, 2005
    Posts: 36478
        
      16
    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

    Joined: Sep 17, 2006
    Posts: 1282
    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.
    Calum Ludwig
    Greenhorn

    Joined: Oct 01, 2007
    Posts: 14
    Could you do something like this in the dateset class?



    SCJP 1.4<br />SCJD
     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: format double
     
    Similar Threads
    Formatter class problem
    Printf
    Actual parameter checking
    Using return
    Not clear which is right Please clarify