File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


JavaRanch » Java Forums » Java » Beginning Java
Reply Bookmark ""printf" applied to a float using a Parameter" Watch ""printf" applied to a float using a Parameter" New topic
Author

"printf" applied to a float using a Parameter

Wolfgang Obi
Ranch Hand

Joined: Dec 05, 2005
Posts: 134
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

Joined: Oct 13, 2005
Posts: 32833
    
    4
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

Joined: Oct 13, 2005
Posts: 32833
    
    4
. . . 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

Joined: Dec 05, 2005
Posts: 134
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

Joined: Oct 13, 2005
Posts: 32833
    
    4
Glad to be able to help
Jesper de Jong
Java Cowboy
Bartender

Joined: Aug 16, 2005
Posts: 12953
    
    3

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.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Wolfgang Obi
Ranch Hand

Joined: Dec 05, 2005
Posts: 134
thanks jasper.
looks less complicated :-)

cheers-

-W.O.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 32833
    
    4
Another way you could have solved this:
I wish I had thought of that
Quite right.
 
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to run our stuff on 16 servers instead of 3.
 
subject: "printf" applied to a float using a Parameter
 
Similar Threads
format
debug - breakpoint -- only stop when a variable is a certain value?
Doubt in format and printf
Problem with printf output
validating if the input value is float??