Author |
Message |
ScarletSkyFury

|
Posted: 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 |
|
|
|
|
 |
Sponsor Sponsor

|
|
 |
HeavenAgain

|
Posted: 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 |
|
|
|
|
 |
Tony

|
Posted: 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. |
Tony's programming blog. DWITE - a programming contest. |
|
|
|
 |
ScarletSkyFury

|
Posted: Wed Nov 07, 2007 7:58 pm Post subject: Re: Limiting decimals on a float output |
|
|
Im using JOptionPane ><' |
|
|
|
|
 |
HeavenAgain

|
Posted: Wed Nov 07, 2007 11:09 pm Post subject: RE:Limiting decimals on a float output |
|
|
code: | import java.util.Formatter;
Formatter formatter = new Formatter();
formatter.format("%.2f",23.556849); |
check out the api on formatter class |
|
|
|
|
 |
syntax_error

|
Posted: 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
Java: |
double qwer= 12. 34234;
System. out. printf("%.2d",qwer );
|
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] |
|
|
|
|
 |
HeavenAgain

|
Posted: 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
Java: |
double qwer= 12. 34234;
System. out. printf("%.2f",qwer );
|
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  |
|
|
|
|
 |
McKenzie

|
Posted: 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). |
|
|
|
|
 |
Sponsor Sponsor

|
|
 |
syntax_error

|
Posted: 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 |
|
|
|
|
 |
|