Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

problem with array

 
naga raaju
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
System.out.printf("%5.2f", myVariable);

This is not valid in java.
 
Ulf Dittmer
Rancher
Posts: 42967
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2854
10
Fedora Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
naga raaju
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1374
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 48652
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 48652
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you really want your array to hold numbers to two decimal places, that sounds like a job for the BigDecimal class.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic