aspose file tools*
The moose likes Beginning Java and the fly likes problem with array Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "problem with array" Watch "problem with array" New topic
Author

problem with array

naga raaju
Greenhorn

Joined: Mar 14, 2008
Posts: 29
hi guys

i have an array
double[] value={23,332,44,22,22};

i print this the out put is like

23.0 332.0 44.0 22.0 22.0

what my question is
double[] value={23.22211,332.22323,44.23323,22.32232,22.3232};

is there any possible to take only two digits after point like this ==> 23.22 ,332.22,44.23,22,32,22.32 when declare the value array.

can anybody give idea.

by
Naga
Richard Chambers
Ranch Hand

Joined: Apr 03, 2008
Posts: 40
I'm just learing java and don't really have a clue yet,
but I'll throw in my 2 cents worth to contribute

It sounds like you are looking for the 2 decimals in the output?

you do that with the printf

would look something like this

System.out.printf("%5.2f", myVariable);

Hope that helps

if you're asking something deeper I haven't got to that chapter yet
naga raaju
Greenhorn

Joined: Mar 14, 2008
Posts: 29
hi thanks for reply
what my question is when i assigned to array that array will take only two decimals.but that array should double.

any idea in java
Shivit Agarwal
Ranch Hand

Joined: Feb 28, 2008
Posts: 82
System.out.printf("%5.2f", myVariable);

This is not valid in java.


Have the determination of mirror which never fails to reflect in spite of being broken into pieces.<br /> <br />Kiss the hands you cannot bite.<br /> <br />An Optimist is one who starts taking a bath when he accidentally falls into the water.
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42951
    
  72
Originally posted by Shivit Agarwal:
System.out.printf("%5.2f", myVariable);

This is not valid in java.


Sure it is. This was introduced in Java 5.
Amit Ghorpade
Bartender

Joined: Jun 06, 2007
Posts: 2718
    
    6

Hi, well Richard is certainly correct,
the only thing he missed is the method, instead of printf, I would suggest

for further reference, just see this

Hope this helps
[ April 12, 2008: Message edited by: Amit Ghorpade ]

SCJP, SCWCD.
|Asking Good Questions|
naga raaju
Greenhorn

Joined: Mar 14, 2008
Posts: 29
thanks for your cooperation.

what my problem is is there any possible
to declare a double array upto two decimal points .


thanks
naga
Vikas Kapoor
Ranch Hand

Joined: Aug 16, 2007
Posts: 1374
Originally posted by naga raaju:
is there any possible
to declare a double array upto two decimal points .


No.
To get precision upto two decimal points anyhow you have to format it.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 40061
    
  28
Ulf Dittmer is correct; the printf method of whatever System.out is (I think it's a PrintStream, but you would have to check the API) was introduced in J5. The format method was introduced at the same time; both of them do something very similar, but you will have to check the API to be sure.

Both of them actually call the Formatter class (as far as I can remember).

As for precision of a double: as long as you stay within a range from Double.MAX_VALUE and Double.MIN_NORMAL_VALUE (at least I think that's what it is called) the precision is fixed at 53 bits, which is equivalent to 53*log10(2) decimal digits which is 15.954589770191003346328161420398 approximately. Well, nearly 16. So whatever you write in a double, you get it accurate to nearly 16 digits; 223.3331 is less than 16 digits, and 15.954589770191003346328161420398 is more than 16 digits, so the last part of this number will disappear into cyber-limbo. The precision of what you store and the precision of what you display can be differentprints:
D:\JavaPrograms\Beginnings>javac -d . decimalformatDemo.java

D:\JavaPrograms\Beginnings>java trivia.DecimalFormatDemo
15.954589770191003000000000000000
15.95
Be grateful for small mercies; when I learned Java it was J1.4.2 and we had to use a DecimalFormat object.
[ April 12, 2008: Message edited by: Campbell Ritchie ]
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 40061
    
  28
If you really want your array to hold numbers to two decimal places, that sounds like a job for the BigDecimal class.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: problem with array