Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

How to write out double with only 2 digits?

0 views
Skip to first unread message

Fred

unread,
Jan 3, 2002, 9:27:30 AM1/3/02
to
I have a variable which is double but I would like to write it out with
only 2 digits! How do I do that? Many thanks in advance!

Fred

Jon Skeet

unread,
Jan 3, 2002, 9:54:22 AM1/3/02
to
Fred <""fred\"@(none)> <Fred <""fred\"@(none)>> wrote:
> I have a variable which is double but I would like to write it out with
> only 2 digits! How do I do that? Many thanks in advance!

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

Doug

unread,
Jan 3, 2002, 9:55:17 AM1/3/02
to
In article <3c3469fb$1...@newsflash.abo.fi>, Fred <""fred\"@(none)> wrote:
>I have a variable which is double but I would like to write it out with
>only 2 digits! How do I do that? Many thanks in advance!

See Java.Text.NumberFormat.

doug

Manfred Leonhardt

unread,
Jan 3, 2002, 10:23:59 AM1/3/02
to
Hi Fred,

I assume you mean 2 significant digits. Try the following:

DecimalFormat df = new DecimalFormat( "#.00" );
System.out.println( df.format( "23.4567898" ) );

Regards,
Manfred.

Fred

unread,
Jan 3, 2002, 12:43:25 PM1/3/02
to
Manfred Leonhardt wrote:

What should I need to import?

Roedy Green

unread,
Jan 3, 2002, 2:01:53 PM1/3/02
to
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

On Thu, 03 Jan 2002 16:27:30 +0200, Fred <""fred\"@(none)> wrote or
quoted :

>I have a variable which is double but I would like to write it out
>with only 2 digits! How do I do that? Many thanks in advance!

See "conversion" in the Java glossary.

-----BEGIN PGP SIGNATURE-----
Version: PGPfreeware 7.0.3 for non-commercial use <http://www.pgp.com>

iQA/AwUBPDSqlO9ipV+Syz7dEQIgSACfbL6bq8iYs4HZF4yg2cpvMkDgug8AnRxa
O55kKNi7P7GlVwSYwZp6+PYZ
=InF6
-----END PGP SIGNATURE-----

--
eagerly seeking telecommuting programming work.
canadian mind products, roedy green
the java glossary at
http://www.mindprod.com/gloss.html
or http://209.139.205.39

Jon A. Cruz

unread,
Jan 3, 2002, 2:05:17 PM1/3/02
to
Fred wrote:

import java.text.DecimalFormat;

--
Jon A. Cruz
http://www.geocities.com/joncruz/action.html

David Bolton

unread,
Jan 5, 2002, 12:08:37 AM1/5/02
to
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)

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...

David Bolton

unread,
Jan 5, 2002, 12:27:40 AM1/5/02
to
Hello Stan,

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...

Tom Davies

unread,
Jan 5, 2002, 1:06:52 AM1/5/02
to
David Bolton wrote:

> 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

Mishko

unread,
Jan 5, 2002, 7:26:06 PM1/5/02
to
Just remove quotes "" on the number, it works..

"Tom Davies" <tomd...@optushome.com.au> wrote in message
news:3C3697FC...@optushome.com.au...

Roedy Green

unread,
Jan 11, 2002, 11:38:51 PM1/11/02
to
See conversion double > string in the Java glossary for sample code.

--
eagerly seeking telecommuting programming work.
canadian mind products, roedy green

the java glossary is at

0 new messages