| Author |
another question on arrays
|
Wolfgang Obi
Ranch Hand
Joined: Dec 05, 2005
Posts: 134
|
|
hi, if this is a given array: Integer[] ir = {12, 45678, 90123, 3}; how do i rewrite this loop: to get following output: ----------------- 12 (...e.g. Euro) 45.678 90.123 3 ----------------- i am trying to get the programme to print out numbers (without comma, or decimal separators : as seen in Array "ir" ) as currency sums... ....however: the sums should be formatted the way some european countries do...(i.e. not with commas as is done in the US for example - but with decimal points)... what functions/methods etc do i need to append (to replace the " *** " )... i would also appreciate somene explaining to me where (in the API ? ) i can get more help..... i've looked through the classes: DecimalFormat and Formatter, as well as Locale..... but it's gotten me even more confused... i'd appreciate some help. thanx! -W.O.
|
 |
Ajay A Patil
Greenhorn
Joined: Apr 13, 2006
Posts: 22
|
|
> what functions/methods etc do i need to append (to replace the " *** " )... DecimalFormat df = (DecimalFormat) NumberFormat.getInstance(Locale.GERMAN); System.out.println(df.format(ir[i]));
|
 |
Justin Fox
Ranch Hand
Joined: Jan 24, 2006
Posts: 802
|
|
|
***.getCurrencyInstance***
|
You down with OOP? Yeah you know me!
|
 |
Bear Bibeault
Author and ninkuma
Marshal
Joined: Jan 10, 2002
Posts: 56185
|
|
"Monk Fox", you have previously been warned on multiple occasions regarding adjusting your display name to meet JavaRanch standards. This is not optional, and this is your final warning. Adjust your display name to comply with the required standards prior to your next post. Failure to comply will result in the removal of your account. bear JavaRanch Sheriff
|
[Smart Questions] [JSP FAQ] [Books by Bear] [Bear's FrontMan] [About Bear]
|
 |
Wolfgang Obi
Ranch Hand
Joined: Dec 05, 2005
Posts: 134
|
|
oh, thanx a million Ajay / Monk.... looks like a workable solution, can anyone tell me how i would do it using "printf" ....as i was told it would be more efficient...(how true is this?) ....and are there any advantages of using "printf" over the way suggested by Ajay/ Monk ? thanx in advance. -W.O.
|
 |
Garrett Rowe
Ranch Hand
Joined: Jan 17, 2006
Posts: 1295
|
|
Here is an exmple usinng printf(), although I have no idea about differences in efficiency The flags & conversion syntax used in the printf() method are located in the documentation for the Formatter class. They are a little confusing as you undoubtedly have already seen. The most straightforward way IMO is using the DecimalFormat class as has already been suggested. But it can be done either way.
|
Some problems are so complex that you have to be highly intelligent and well informed just to be undecided about them. - Laurence J. Peter
|
 |
Campbell Ritchie
Sheriff
Joined: Oct 13, 2005
Posts: 32665
|
|
You use letters preceded by % characters in a String to represent an output. So %d will print out a decimal integer calue of how mush money you should send me. You can use different figures after the % to have the number take up a certain number of spaces, with or without leading 000 or +-. %n is new line, and %6.2f is a fixed-point decimal number occupying six spaces, two after the decimal point (eg 123.45 takes up six spaces). There are hundreds of different combinations available; look in the API spceification for the java.util.Formatter class. [ April 17, 2006: Message edited by: Campbell Ritchie ]
|
 |
Fox Trot
Greenhorn
Joined: Apr 17, 2006
Posts: 5
|
|
lol printf is goofy, just use println or print... C and C++ use printf.... Monk...
|
 |
Wolfgang Obi
Ranch Hand
Joined: Dec 05, 2005
Posts: 134
|
|
thanx alot everyone,.... you've been a great help..... -W.O.
|
 |
 |
|
|
subject: another question on arrays
|
|
|