Forums Register Login

problem with array

+Pie Number of slices to send: Send
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
+Pie Number of slices to send: Send
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
+Pie Number of slices to send: Send
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
+Pie Number of slices to send: Send
System.out.printf("%5.2f", myVariable);

This is not valid in java.
+Pie Number of slices to send: Send
 

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.
+Pie Number of slices to send: Send
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 ]
+Pie Number of slices to send: Send
thanks for your cooperation.

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


thanks
naga
+Pie Number of slices to send: Send
 

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.
+Pie Number of slices to send: Send
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 ]
+Pie Number of slices to send: Send
If you really want your array to hold numbers to two decimal places, that sounds like a job for the BigDecimal class.
We can walk to school together. And we can both read this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com


reply
reply
This thread has been viewed 1022 times.
Similar Threads
double data type and output
To get Maximum and minimum value from an array
confusing stuff in wrapper classes
how to convert double to double array
Getting rid of unwanted decimal numbers
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 28, 2024 12:07:37.