|
> I expect you didn't notice the original source of this thread, where the > questioner was asking why he or she got something like 8.23999999999993755 > instead of 8.24, after a simple calculation. This is not the original question - the Mangesh wants to round the floating point to two decimal places for use in both display and later in validation. I think that Mangesh fully understands what floating point numbers are. Original Question > I need to display a float value on the screen with 2 decimal points and later use that value to perform validations. The > problem is that the float value which calculated has 6plus decimal precision which is totally unwarranted. for e.g. > 3.00 - 1.70 - 0.20 - 0.20 = 0.89999999... instead of 0.90 > Since my validations are based on 0.90 there is a mismatch. > > Is there any way in which I can calculate the float value without unnecessary precision or at least display the right value of > > 0.90 > Mangesh Jesus.Bautista@chase.com showed a good way for formating to String for display using DecimalFormat, e.g. > > DecimalFormat twoDigits = new DecimalFormat( " 0.00" ); > All I was suggesting was two ways to get a float with two decimal places... e.g. > > float roundedInt = Float.parseFloat(twoDigits.format( average )); > To reuse the string, However as Sting and DecimalFormat are classes it might be more efficient to use primitives - e.g. > > int temp = (int)(average * 100); > average = (float)temp / 100; > Which converts without creating a string. If you multiply any float by one hundred and turn in into an integer it will be a whole number (by definition). If you divide any whole number by one hundred and cast it into a float it will have at most two decimal places. > Floating point numbers > work in binary, not in decimal. So dividing by 100 is not "just a two-digit > shift" in floating point, that would be dividing by 4. Java data types are decimal not binary. The underlying implementation might be binary but the float is a decimal number - dividing it by one hundred is a two digit shift. > And by the way, > 323/34 is no more irrational than 422/100. Both are rational numbers. true - but is 8.23999999999993753 ? ----- Original Message ----- From: Clapham, Paul <pclapham@core-mark.com> To: <JAVA400-L@midrange.com> Sent: Friday, March 02, 2001 4:41 PM Subject: RE: Decimal rounding of a float data type in Java. > I expect you didn't notice the original source of this thread, where the > questioner was asking why he or she got something like 8.23999999999993755 > instead of 8.24, after a simple calculation. This IS how floating point > numbers work, regardless of your innumerate denial. Floating point numbers > work in binary, not in decimal. So dividing by 100 is not "just a two-digit > shift" in floating point, that would be dividing by 4. And by the way, > 323/34 is no more irrational than 422/100. Both are rational numbers. > > -----Original Message----- > From: James Donkin [mailto:James@DuMaurier.co.uk] > Sent: March 2, 2001 01:25 > To: JAVA400-L@midrange.com > Subject: Re: Decimal rounding of a float data type in Java. > > > > > Well, not exactly. Your "average" doesn't become 4.22, it becomes > something > > that's extremely close to 4.22. Floating point numbers can't express > *any* > > fraction exactly unless it's a power of 0.5. So don't be surprised if you > > do "System.out.println(average)" and you find something like > > "4.2200000000033775" appearing. > e.g. average = 422 / 100; > > This will always give 4.22. The time you get problems is with 323 / 34 or > some other fraction which generates an irrational number. Saying that > > float average = 422 / 100; > > will give 4.22000032 is like saying that > > float average = 4.22; > > will leave average = 4.2200042. Dividing by 100 just generates a two digit > shift. > > +--- > | 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.