aspose file tools*
The moose likes Beginning Java and the fly likes Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
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: 39380
    
  28
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: 39380
    
  28
. . . 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: 39380
    
  28
Glad to be able to help
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14266
    
  21

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 8 API documentation
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: 39380
    
  28
Another way you could have solved this:
I wish I had thought of that
Quite right.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: "printf" applied to a float using a Parameter