Fred
Have a look at java.text.DecimalFormat
--
Jon Skeet - <sk...@pobox.com>
http://www.pobox.com/~skeet/
If replying to the group, please do not mail me too
See Java.Text.NumberFormat.
doug
I assume you mean 2 significant digits. Try the following:
DecimalFormat df = new DecimalFormat( "#.00" );
System.out.println( df.format( "23.4567898" ) );
Regards,
Manfred.
What should I need to import?
Exception in thread "main" java.lang.IllegalArgumentException: Cannot format
given Object as a Number
at java.text.NumberFormat.format(NumberFormat.java:209)
at java.text.Format.format(Format.java:121)
at SigDig.main(SigDig.java:9)
Here's the complete file that I compiled and tried to run:
import java.text.DecimalFormat;
class SigDig {
public SigDig() {}
public static void main(String ar[]) {
DecimalFormat df = new DecimalFormat( "#.00" );
System.out.println( df.format( "23.4567898" ) );
}
}
Anyone know what's wrong?
Best regards,
David
www.ideaplace.org/dlb.html
Jon A. Cruz <jon...@geocities.com> wrote in message
news:3C34AB6D...@geocities.com...
You need to include the package in the compiling. Assuming that you're
using Windows 98 as I am, here's what I typed in dos to compile the code:
C:\j\a>c:\jdk\bin\javac fm.java pkg\Fred.java
This will compile both fm and Fred at the same time...although Fred can be
compiled beforehand. I also ran the code with the dos line:
C:\j\a>c:\jdk\bin\java -cp . fm
doit
(The C:\j\a is where I have your fm and pkg, and jdk is the directory of my
jdk 1.3.1.)
Best regards,
David
www.ideaplace.org/dlb.html
Jon A. Cruz <jon...@geocities.com> wrote in message
news:3C34AB6D...@geocities.com...
> Hello,
> When I compiled and ran the posted code, I got the error:
>
> Exception in thread "main" java.lang.IllegalArgumentException: Cannot format
> given Object as a Number
> at java.text.NumberFormat.format(NumberFormat.java:209)
> at java.text.Format.format(Format.java:121)
> at SigDig.main(SigDig.java:9)
DecimalFormat doesn't format Strings, only numbers.
Form the Javadoc for Format.format:
"Throws:
IllegalArgumentException - if the Format cannot format the given object"
Tom
"Tom Davies" <tomd...@optushome.com.au> wrote in message
news:3C3697FC...@optushome.com.au...