Computer Science Canada Limiting decimals on a float output |
Author: | ScarletSkyFury [ Wed Nov 07, 2007 2:14 pm ] |
Post subject: | Limiting decimals on a float output |
Okay so basicaly I want the result of calculation to come out with max 2 decimals Eg. 23.55 and not 23.556849 Any ideas? Thanks in advanced |
Author: | HeavenAgain [ Wed Nov 07, 2007 3:28 pm ] |
Post subject: | RE:Limiting decimals on a float output |
System.out.printf("%.2f",23.556849); or look up Math.round , but printf is better |
Author: | Tony [ Wed Nov 07, 2007 3:59 pm ] |
Post subject: | RE:Limiting decimals on a float output |
well Math.round will... round. printf is used for formatting. The latter is probably a better approach in this case. |
Author: | ScarletSkyFury [ Wed Nov 07, 2007 7:58 pm ] |
Post subject: | Re: Limiting decimals on a float output |
Im using JOptionPane ><' |
Author: | HeavenAgain [ Wed Nov 07, 2007 11:09 pm ] | ||
Post subject: | RE:Limiting decimals on a float output | ||
check out the api on formatter class |
Author: | syntax_error [ Fri Nov 09, 2007 9:28 pm ] | ||
Post subject: | Re: Limiting decimals on a float output | ||
err am I allow to ask a question related on the same topic here if not please forgive me ![]() but I was trying the printf and i wanted to do something such as this
and i do not get any syntax error however when i try to run the program i get this error Quote: IllegalFormatPrecisionException: 2 at java.util.Formatter$FormatSpecifier.checkInteger(Unknown Source) at java.util.Formatter$FormatSpecifier.<init>(Unknown Source) at java.util.Formatter.parse(Unknown Source) at java.util.Formatter.format(Unknown Source) at java.io.PrintStream.format(Unknown Source) at java.io.PrintStream.printf(Unknown Source) at QWER.main(QWER.java:4) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) at java.lang.reflect.Method.invoke(Unknown Source) can i not format a double or something? [/quote] |
Author: | HeavenAgain [ Fri Nov 09, 2007 10:21 pm ] | ||
Post subject: | RE:Limiting decimals on a float output | ||
d doesnt not stand for double, it stands for "digit" or something like that, it have to be a "number without floating point", for decimal value, you use %f instead so your code would be something like
there are also a few other ones, like %s (String), %X , %x (hex in upper- or lower-case), %o (octol), %f (floating point, it round up), %d (int).... check the api ![]() |
Author: | McKenzie [ Sat Nov 10, 2007 10:47 am ] |
Post subject: | Re: Limiting decimals on a float output |
For the record the d in %d stands for decimal. As in binary, octal, decimal, hexadecimal. It's a throw-back from C. In C it was no big deal because float was the default real number. If you do want to use round it looks like Math.round(x * 100) / 100 (printf is still a better call). |
Author: | syntax_error [ Sat Nov 10, 2007 8:59 pm ] |
Post subject: | Re: Limiting decimals on a float output |
omg that was a very stupid mistake by me thank you all very much |