On Tue, Apr 12, 2011 at 5:55 PM, Gabriel <[email protected]> wrote: > I have a calculator app and I want to round the number after comma. > > Like, if the result is 15,33333 I want to keep it on 15,33 or 15,3 > > Does any know how to do that? > > Thanks, I really appreciate the help
Do you simply want to format such number, by chopping the tail or populating it with zeros? (1.535 becomes 1.53 or 1.53500000 depending on your format) or Do you want to round it (up, down, or half-up) like 0.005 rounded to 2 decimal places becomes 0.01 or, 0 (0.00) depending on your chosen rounding? The method you tried below is rounding half-up (by using Math.round()). You can make it more generic by parametrising 100 (2 decimal places), but remember that by using doubles you will at some point encounter precision issues with rounding. Most scientific calculators are using floating point numbers and nobody minds. Will your calculator always display only 2 decimal places? Daniel -- You received this message because you are subscribed to the Google Groups "Android Developers" group. To post to this group, send email to [email protected] To unsubscribe from this group, send email to [email protected] For more options, visit this group at http://groups.google.com/group/android-developers?hl=en

