Here is some example code using BigDecimal to give you three decimal places.

float number = 9.9f;
System.out.println("number: " + number);
java.math.BigDecimal number2 = new java.math.BigDecimal(number);
System.out.println("number2: " + number2.toString());
java.math.BigDecimal number3 = number2.setScale(3,
        java.math.BigDecimal.ROUND_CEILING);
System.out.println("number3: " + number3.toString());
float number4 = number3.floatValue();
System.out.println("number4: " + number4);

If you run this in a test program you'll see that what you would want in
this case is number3.  If you want the extra decimals that are 0 value
dropped off you would want number4.

Dale Nicholson

-----Original Message-----
From: Kenny G. Dubuisson, Jr. [mailto:[EMAIL PROTECTED]]
Sent: Friday, October 11, 2002 1:51 PM
To: [EMAIL PROTECTED]
Subject: Strange number formatting behavior


I am trying to output number in a formatted style and keep having strange
behavior.  If I just output the float value, I get "9.9" for example.  If I
format it to 3 decimals, I get "9.912"; it's adding values that aren't there
in the actual number.  Here is what I get

NumberFormat form = NumberFormat.getInstance();
form.setMinimumIntegerDigits = 1;
form.setMinimumFractionDigits = 3;
form.setMaximumFractionDigits = 3;
float myNumber = 9.9;
String myNumberFormatted = form.format(myNumber);  //Output is "9.912"

This absolutely makes no sense.  Anyone got any ideas?  Thanks,
Kenny

===========================================================================
To unsubscribe: mailto [EMAIL PROTECTED] with body: "signoff
JSP-INTEREST".
For digest: mailto [EMAIL PROTECTED] with body: "set JSP-INTEREST
DIGEST".
Some relevant FAQs on JSP/Servlets can be found at:

 http://archives.java.sun.com/jsp-interest.html
 http://java.sun.com/products/jsp/faq.html
 http://www.esperanto.org.nz/jsp/jspfaq.jsp
 http://www.jguru.com/faq/index.jsp
 http://www.jspinsider.com

===========================================================================
To unsubscribe: mailto [EMAIL PROTECTED] with body: "signoff JSP-INTEREST".
For digest: mailto [EMAIL PROTECTED] with body: "set JSP-INTEREST DIGEST".
Some relevant FAQs on JSP/Servlets can be found at:

 http://archives.java.sun.com/jsp-interest.html
 http://java.sun.com/products/jsp/faq.html
 http://www.esperanto.org.nz/jsp/jspfaq.jsp
 http://www.jguru.com/faq/index.jsp
 http://www.jspinsider.com

Reply via email to