p2p.wrox.com Forums

Need to download code?

View our list of code downloads.


  Return to Index  

beginning_java thread: Re: printf function


Message #1 by Next Step <nextstep@t...> on Thu, 04 Apr 2002 11:47:10 +0700
Hi,
Thanks. Thats what i looking for. I dont want to use external package just 
for this simple task. It will make trouble when moving to another container 
(actually it used on jsp).

At 07:44 PM 4/2/2002 +0000, you wrote:
> > Hi all,
>Is there any way to display double/float number in special format
>
>Try the java.text.DecimalFormat class -
>
>import java.text.DecimalFormat;
>
>class Pct {
>   public static void main(String[] arg) {
>     long total = 23785;
>     long in = 18895;
>     double pct = ((double)in/total)*100;
>
>     // What we want is only 79.4 %
>     // Then use the DecimalFormat class
>     DecimalFormat nf = new DecimalFormat("###,###.0");
>
>     System.out.println("in : " + nf.format(pct) + " %");
>   }
>}

Message #2 by "Kenneth Lee" <kenneth_a_lee@y...> on Tue, 2 Apr 2002 19:44:35
> Hi all,
Is there any way to display double/float number in special format

Try the java.text.DecimalFormat class - 

import java.text.DecimalFormat;

class Pct {
  public static void main(String[] arg) {
    long total = 23785;
    long in = 18895;
    double pct = ((double)in/total)*100;

    // What we want is only 79.4 %
    // Then use the DecimalFormat class
    DecimalFormat nf = new DecimalFormat("###,###.0"); 

    System.out.println("in : " + nf.format(pct) + " %");
  }
}

Look up the class definition in the java docs.  This will tell you when to 
use # or 0 to represent numbers.

Hope that helps,

Ken
Message #3 by "Matthew Wood" <matthew.wood@b...> on Tue, 2 Apr 2002 10:55:03
It isn't pretty, but it works

		 		 System.out.println("in : " + (float)
Math.round(pct*10)/10 + " %");

Matt.
> Hi all,
Is there any way to display double/float number in special format, ie. 
only 
display 1 digit after decimal. Consider this following code :

------<cut>----------
class Pct {
	public static void main(String[] arg) {
		long total = 23785;
		long in = 18895;
		double pct = ((double)in/total)*100;

		System.out.println("in : " + pct + " %");
	}
}
----------------------
Will give output :
in : 79.44082404877022 %

What we want is only 79.4 %

TIA.

Message #4 by Next Step <nextstep@t...> on Mon, 01 Apr 2002 12:45:12 +0700
Hi all,
Is there any way to display double/float number in special format, ie. only 
display 1 digit after decimal. Consider this following code :

------<cut>----------
class Pct {
	public static void main(String[] arg) {
		long total = 23785;
		long in = 18895;
		double pct = ((double)in/total)*100;

		System.out.println("in : " + pct + " %");
	}
}
----------------------
Will give output :
in : 79.44082404877022 %

What we want is only 79.4 %

TIA.

Message #5 by "John B. Spindler" <spindler@k...> on Tue, 2 Apr 2002 08:19:02 +0900
Check this out:

http://developer.java.sun.com/developer/technicalArticles/Programming/sp
rintf/
Or
http://www.braju.com/
Or
http://www.sharkysoft.com/software/lava/docs/lavadocs/lava/clib/stdio/do
c-files/
And one more:
http://sharkysoft.com/software/java/printf/docs/javadocs/lava/clib/Stdio
.html

Hope that helps!

John B. Spindler
Systems Engineer
KDDI Okinawa Co., Ltd.
Local: 926-2754
Commercial: 011-81-98-926-2754
spindler@k...


-----Original Message-----
From: Next Step [mailto:nextstep@t...] 
Sent: Monday, April 01, 2002 2:45 PM
To: Beginning Java
Subject: [beginning_java] printf function


Hi all,
Is there any way to display double/float number in special format, ie.
only 
display 1 digit after decimal. Consider this following code :

------<cut>----------
class Pct {
	public static void main(String[] arg) {
		long total = 23785;
		long in = 18895;
		double pct = ((double)in/total)*100;

		System.out.println("in : " + pct + " %");
	}
}
----------------------
Will give output :
in : 79.44082404877022 %

What we want is only 79.4 %

TIA.


---
Do you need true END-TO-END (e2e) Java Messaging (JMS)?  Softwired
offers industry's ONLY complete 100% JMS messaging solutions for your
needs TODAY. Visit
http://adtracking.wrox.com/track.asp?x=p2p%2Fe%2Fjava%2Dsoftwired&url=ww
w.softwired-inc.com Our unique product range includes: iBus//Mobile
(wireless data), iBus//MessageBus (IP Multicast), iBus//MessageServer
(store and forward) and much more!




  Return to Index