|
> DecimalFormat twoDigits = new DecimalFormat( " 0.00" ); > > if ( gradeCounter != 0 ) { > average = total / gradeCounter; > > // display output here > JOptionPane.showMessageDialog( null, > "Average is " + twoDigits.format( average ), " Average ", > JOptionPane.INFORMATION_MESSAGE ); > } One way for getting rid of the rounding problem would be to... float roundedInt = Float.parseFloat(twoDigits.format( average )); Or you could... int temp = (int)(average * 100); average = (float)temp / 100; e.g. average = 4.222222222222 temp then = 422 average then becomes 4.22 This feels better to me than using BigDecimal. James Donkin me@theComputer.com ----- Original Message ----- From: <Jesus.Bautista@chase.com> To: <JAVA400-L@midrange.com> Cc: <JAVA400-L@midrange.com>; <vijosh@systsoft.com> Sent: Thursday, March 01, 2001 12:46 PM Subject: Re: Decimal rounding of a float data type in Java. > > Here's an example of displaying decimal value: > > > > DecimalFormat twoDigits = new DecimalFormat( " 0.00" ); > > if ( gradeCounter != 0 ) { > average = total / gradeCounter; > > // display output here > JOptionPane.showMessageDialog( null, > "Average is " + twoDigits.format( average ), " Average ", > JOptionPane.INFORMATION_MESSAGE ); > } > > the first statement declares twoDigits as a reference to an object of > class DecimalFormat. > > DecimalFormat objects are used to format number. In this example we want > to output the variable average with two digits to the right of the decimal > point. > > +--- > | This is the JAVA/400 Mailing List! > | To submit a new message, send your mail to JAVA400-L@midrange.com. > | To subscribe to this list send email to JAVA400-L-SUB@midrange.com. > | To unsubscribe from this list send email to JAVA400-L-UNSUB@midrange.com. > | Questions should be directed to the list owner: joe@zappie.net > +--- > +--- | This is the JAVA/400 Mailing List! | To submit a new message, send your mail to JAVA400-L@midrange.com. | To subscribe to this list send email to JAVA400-L-SUB@midrange.com. | To unsubscribe from this list send email to JAVA400-L-UNSUB@midrange.com. | Questions should be directed to the list owner: joe@zappie.net +---
As an Amazon Associate we earn from qualifying purchases.
This mailing list archive is Copyright 1997-2024 by midrange.com and David Gibbs as a compilation work. Use of the archive is restricted to research of a business or technical nature. Any other uses are prohibited. Full details are available on our policy page. If you have questions about this, please contact [javascript protected email address].
Operating expenses for this site are earned using the Amazon Associate program and Google Adsense.