You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tomcat.apache.org by Lee Chin Khiong <le...@cmks.com.sg> on 2003/10/07 03:37:37 UTC

How to format number

How to format a double in JSP or java ?

for example :

10/3=3.3333333333333

I want to format it to 3.33.


Re: How to format number

Posted by Boris Folgmann <mi...@folgmann.com>.
Lee Chin Khiong wrote:
> How to format a double in JSP or java ?
> 
> for example :
> 
> 10/3=3.3333333333333
> 
> I want to format it to 3.33.

Try realToString(3.33, 2) with this:

    /** Convert a float to a string with fixed number of fraction digits
     */
    public static String realToString(float f, int numFracDigits, Locale
locale)
    {
	NumberFormat fmt = NumberFormat.getInstance(locale);
	fmt.setMaximumFractionDigits(numFracDigits);
	fmt.setMinimumFractionDigits(numFracDigits);
	return fmt.format(f);
    }

or for default locale:

    /** Convert a float to a string with fixed number of fraction digits
     */
    public static String realToString(float f, int numFracDigits)
    {
	NumberFormat fmt = NumberFormat.getInstance();
	fmt.setMaximumFractionDigits(numFracDigits);
	fmt.setMinimumFractionDigits(numFracDigits);
	return fmt.format(f);
    }


-- 
Dipl.-Inf. Boris Folgmann   mailto:boris@folgmann.de
TeamForge GmbH              http://www.teamforge.de
-m-o-d-w-a-r-s-             http://www.modwars.de


---------------------------------------------------------------------
To unsubscribe, e-mail: tomcat-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: tomcat-user-help@jakarta.apache.org


Re: How to format number

Posted by Paul Gregoire <pa...@gregoire.org>.
Look into the java.text package, you'll find a Number formatter there.. or
if you dont want a String, use Math.round()


----- Original Message ----- 
From: "Lee Chin Khiong" <le...@cmks.com.sg>
To: "'Tomcat Users List'" <to...@jakarta.apache.org>
Sent: Monday, October 06, 2003 6:37 PM
Subject: How to format number


> How to format a double in JSP or java ?
>
> for example :
>
> 10/3=3.3333333333333
>
> I want to format it to 3.33.
>
>


---------------------------------------------------------------------
To unsubscribe, e-mail: tomcat-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: tomcat-user-help@jakarta.apache.org


Re: How to format number

Posted by Lawence <al...@yahoo.com>.
There are some non-standard nice library classes you can use for this purpose. Just google it. These classes enable you to format things like you are working with c/c++.
 


Lee Chin Khiong <le...@cmks.com.sg> wrote:
How to format a double in JSP or java ?

for example :

10/3=3.3333333333333

I want to format it to 3.33.



---------------------------------
Do you Yahoo!?
The New Yahoo! Shopping - with improved product search