• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

another question on arrays

 
Wolfgang Obi
Ranch Hand
Posts: 134
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 22
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
> 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
Posts: 802
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
***.getCurrencyInstance***
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64181
83
IntelliJ IDE Java jQuery Mac Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"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
 
Wolfgang Obi
Ranch Hand
Posts: 134
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1296
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Campbell Ritchie
Sheriff
Pie
Posts: 47232
52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
lol printf is goofy, just use println or print...

C and C++ use printf....

Monk...
 
Wolfgang Obi
Ranch Hand
Posts: 134
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanx alot everyone,....

you've been a great help.....

-W.O.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic