• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

"printf" applied to a float using a Parameter

 
Wolfgang Obi
Ranch Hand
Posts: 134
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi ,
i would please like to know if it is possible to replace the "2" (bold/italicised - see below) with a variable for example "afterKomma"?...if yes, how do i implement this, if no, what other (better?) options do i have ?



what i'm asking basically is how can i realise something like this:



thanks
[ June 18, 2006: Message edited by: Wolfgang Obi ]
 
Campbell Ritchie
Sheriff
Pie
Posts: 49367
62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I trust you have been through the API specifications about the % tags? Look in the java.util.Formatter class. Then tell us how you sorted out the problem.
 
Campbell Ritchie
Sheriff
Pie
Posts: 49367
62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
. . . and for variable numbers after the decimal point . . .

Difficult

. . .

You will have to go to the NumberFormat or DecimalFormat classes and see whether you can find any methods which allow you to set digits in the fraction.

There is a silly example on this website from another German chap. Look at it.
 
Wolfgang Obi
Ranch Hand
Posts: 134
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey Ritchie,

thanks once again for all the help and prodding in the right direction ...
here's how i finally solved the problem:


[ June 18, 2006: Message edited by: Wolfgang Obi ]
 
Campbell Ritchie
Sheriff
Pie
Posts: 49367
62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Glad to be able to help
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15354
39
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Another way you could have solved this:

Note that the string "%.2f\n" is just a String object. It does not have to be a literal string; you can pass any other String object instead.
 
Wolfgang Obi
Ranch Hand
Posts: 134
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks jasper.
looks less complicated :-)

cheers-

-W.O.
 
Campbell Ritchie
Sheriff
Pie
Posts: 49367
62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Another way you could have solved this:
I wish I had thought of that
Quite right.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic