How to use decimal format.

737 views
Skip to first unread message

elidan1

unread,
Apr 12, 2000, 3:00:00 AM4/12/00
to
In article <38F53EDC...@crl.tel.co.jp>, Yun Mo
<mo...@crl.tel.co.jp> wrote:
>I want to format a decimal number as:
>
>1.2345E+01
>1.2345E-01
>
>The length of format is fixed. I tried to use following format
but I
>failed.
>
>DecimalFormat df = new DecimalFormat("0.0000E+00;0.0000E-00");
>
>System.out.print(df.format(1.2345e1));
>System.out.print(df.format(1.2345e-1));
>
>I got error message as the following:
>
>Exception in thread "main" java.lang.IllegalArgumentException:
Malformed
>
>exponen
>tial pattern "0.000E-00;0.000E+00"
>
>
>Could you help me ?
>
>Thank you in advace.
>
>
>M.Y. TDC/TEL
>
>
>
>
You do not need to specify the sign of the exponent. It will be
inserted appropriately by java. Also unless you need a different
pattern for negative numbers (not exponents), you do not need a
second pattern.
"0.000E00" is all you need.


elidan1
for mail null IS null
nulle...@nullzahav.net.il
* Sent from RemarQ http://www.remarq.com The Internet's Discussion Network *
The fastest and easiest way to search and participate in Usenet - Free!


Yun Mo

unread,
Apr 13, 2000, 3:00:00 AM4/13/00
to

Yun Mo

unread,
Apr 13, 2000, 3:00:00 AM4/13/00
to elidan1
I appreciate your advice.

Yes, I have done as what you said. The problem is that the length of formatted
number
changes when the number is less than 1 with the format as "0.000E00".
For an example,

0.01230 --> 1.230E-02 ( with the "0.000E00")

In fact, I want the format: 1.230E-2 not 1.230E-02.

No matter whether the number is less than 1 or not, I want to fix
the length of the number, just like in Fortran, C.

Please see the following formatted results with "0.000E00". Due to the change of
the length of the number in the case of number less than 1, the tabled data
looks ugly.

I also tried with the Format class in corejava but it seems there exist some
serious problems.

Anyway, thank you for your message.

Regards,
M.Y.


********* con *********

i== 01 02 03 04 05 06 07
08 09 10 11 12
j
12 0.000E00 0.000E00 0.000E00 0.000E00 0.000E00 0.000E00 0.000E00
0.000E00 0.000E00 0.000E00 0.000E00 0.000E00
11 0.000E00 3.960E00 2.970E00 2.970E00 2.970E00 2.970E00 2.970E00
2.970E00 2.970E00 2.970E00 3.960E00 0.000E00
10 0.000E00 4.950E00 3.960E00 3.960E00 3.960E00 3.960E00 3.960E00
3.960E00 3.960E00 3.960E00 4.950E00 0.000E00
9 0.000E00 4.950E00 3.960E00 3.960E00 3.960E00 3.960E00 3.960E00
3.960E00 3.960E00 3.960E00 4.950E00 0.000E00
8 0.000E00 4.950E00 3.960E00 3.960E00 3.960E00 3.960E00 3.960E00
3.960E00 3.960E00 3.960E00 4.950E00 0.000E00
7 0.000E00 4.950E00 3.960E00 3.960E00 3.960E00 3.960E00 3.960E00
3.960E00 3.960E00 3.960E00 4.950E00 0.000E00
6 0.000E00 4.950E00 3.960E00 3.960E00 3.960E00 3.960E00 3.960E00
3.960E00 3.960E00 3.960E00 4.950E00 0.000E00
5 0.000E00 4.950E00 3.960E00 3.960E00 3.960E00 3.960E00 3.960E00
3.960E00 3.960E00 3.960E00 4.950E00 0.000E00
4 0.000E00 4.950E00 3.960E00 3.960E00 3.960E00 3.960E00 3.960E00
3.960E00 3.960E00 3.960E00 4.950E00 0.000E00
3 0.000E00 4.950E00 3.960E00 3.960E00 3.960E00 3.960E00 3.960E00
3.960E00 3.960E00 3.960E00 4.950E00 0.000E00
2 0.000E00 3.960E00 2.970E00 2.970E00 2.970E00 2.970E00 2.970E00
2.970E00 2.970E00 2.970E00 3.960E00 0.000E00
1 0.000E00 0.000E00 0.000E00 0.000E00 0.000E00 0.000E00 0.000E00
0.000E00 0.000E00 0.000E00 0.000E00 0.000E00

********* fi *********

i== 01 02 03 04 05 06 07
08 09 10 11 12
j
12 1.000E00 9.894E-01 9.257E-01 8.360E-01 7.306E-01 6.165E-01 4.993E-01
3.830E-01 2.698E-01 1.602E-01 5.323E-02 0.000E00
11 1.000E00 9.894E-01 9.257E-01 8.360E-01 7.306E-01 6.165E-01 4.993E-01
3.830E-01 2.698E-01 1.602E-01 5.323E-02 0.000E00
10 1.000E00 9.928E-01 9.311E-01 8.418E-01 7.361E-01 6.213E-01 5.033E-01
3.862E-01 2.721E-01 1.615E-01 5.369E-02 0.000E00
9 1.000E00 9.943E-01 9.344E-01 8.458E-01 7.402E-01 6.251E-01 5.066E-01
3.888E-01 2.740E-01 1.627E-01 5.407E-02 0.000E00
8 1.000E00 9.951E-01 9.364E-01 8.484E-01 7.431E-01 6.279E-01 5.091E-01
3.909E-01 2.755E-01 1.636E-01 5.439E-02 0.000E00
7 1.000E00 9.956E-01 9.376E-01 8.502E-01 7.452E-01 6.300E-01 5.111E-01
3.925E-01 2.768E-01 1.644E-01 5.466E-02 0.000E00
6 1.000E00 9.958E-01 9.383E-01 8.513E-01 7.465E-01 6.316E-01 5.126E-01
3.939E-01 2.778E-01 1.651E-01 5.490E-02 0.000E00
5 1.000E00 9.958E-01 9.385E-01 8.518E-01 7.473E-01 6.326E-01 5.137E-01
3.950E-01 2.788E-01 1.657E-01 5.511E-02 0.000E00
4 1.000E00 9.955E-01 9.380E-01 8.515E-01 7.475E-01 6.332E-01 5.146E-01
3.959E-01 2.796E-01 1.663E-01 5.531E-02 0.000E00
3 1.000E00 9.946E-01 9.365E-01 8.503E-01 7.470E-01 6.333E-01 5.152E-01
3.968E-01 2.805E-01 1.669E-01 5.553E-02 0.000E00
2 1.000E00 9.916E-01 9.326E-01 8.473E-01 7.450E-01 6.324E-01 5.150E-01
3.971E-01 2.809E-01 1.673E-01 5.568E-02 0.000E00
1 1.000E00 9.916E-01 9.326E-01 8.473E-01 7.450E-01 6.324E-01 5.150E-01
3.971E-01 2.809E-01 1.673E-01 5.568E-02 0.000E00


elidan1 wrote:

> In article <38F53EDC...@crl.tel.co.jp>, Yun Mo
> <mo...@crl.tel.co.jp> wrote:

elidan1

unread,
Apr 13, 2000, 3:00:00 AM4/13/00
to
The only thing that comes to my mind is to split the output into
two cases: numbers>1 and numbers<1. For the positive exponent you
can use the same pattern as before and for negative exponents you
can use "0.000E0" (there can be any number of zeros there), so
when the minus sign is inserted, the field lenght will be the
same. Kind of hacked, but it should work. I am not aware of a way
to display the + sign itself. Unfortunately, not every thing
Fortran can do, java can do too.

Steve Chapel

unread,
Apr 13, 2000, 3:00:00 AM4/13/00
to
"Yun Mo" <mo...@crl.tel.co.jp> wrote in message
news:38F5ACF7...@crl.tel.co.jp...

> I appreciate your advice.
>
> Yes, I have done as what you said. The problem is that the length of
formatted
> number
> changes when the number is less than 1 with the format as "0.000E00".
> For an example,
>
> 0.01230 --> 1.230E-02 ( with the "0.000E00")
>
> In fact, I want the format: 1.230E-2 not 1.230E-02.
>
> No matter whether the number is less than 1 or not, I want to fix
> the length of the number, just like in Fortran, C....

Bug Id #4191595 is an RFE to allow using DecimalFormat to line up
columns of numbers. If you want this capability in Java, sign up with
the Java Developer Connection and vote for this RFE.


Yun Mo

unread,
Apr 14, 2000, 3:00:00 AM4/14/00
to
It seems that the problem has not resolved.
Thanks.

Steve Chapel wrote:

> "Yun Mo" <mo...@crl.tel.co.jp> wrote in message
> news:38F5ACF7...@crl.tel.co.jp...

> > I appreciate your advice.
> >
> > Yes, I have done as what you said. The problem is that the length of
> formatted
> > number
> > changes when the number is less than 1 with the format as "0.000E00".
> > For an example,
> >
> > 0.01230 --> 1.230E-02 ( with the "0.000E00")
> >
> > In fact, I want the format: 1.230E-2 not 1.230E-02.
> >
> > No matter whether the number is less than 1 or not, I want to fix

Yun Mo

unread,
Apr 14, 2000, 3:00:00 AM4/14/00
to
It may be a temporary solution.

Thank you for your suggestion.

elidan1 wrote:

> The only thing that comes to my mind is to split the output into
> two cases: numbers>1 and numbers<1. For the positive exponent you
> can use the same pattern as before and for negative exponents you
> can use "0.000E0" (there can be any number of zeros there), so
> when the minus sign is inserted, the field lenght will be the
> same. Kind of hacked, but it should work. I am not aware of a way
> to display the + sign itself. Unfortunately, not every thing
> Fortran can do, java can do too.
>

Steve Chapel

unread,
Apr 14, 2000, 3:00:00 AM4/14/00
to
Well, that's why I suggested voting for the RFE.
That's about all we can do towards getting the problem resolved.

"Yun Mo" <mo...@crl.tel.co.jp> wrote in message

news:38F680C6...@crl.tel.co.jp...


> It seems that the problem has not resolved.
> Thanks.
>
> Steve Chapel wrote:
>
> > "Yun Mo" <mo...@crl.tel.co.jp> wrote in message
> > news:38F5ACF7...@crl.tel.co.jp...

> > > I appreciate your advice.
> > >
> > > Yes, I have done as what you said. The problem is that the length
of
> > formatted
> > > number
> > > changes when the number is less than 1 with the format as
"0.000E00".
> > > For an example,
> > >
> > > 0.01230 --> 1.230E-02 ( with the "0.000E00")
> > >
> > > In fact, I want the format: 1.230E-2 not 1.230E-02.
> > >
> > > No matter whether the number is less than 1 or not, I want to fix

Dirk Bosmans

unread,
Apr 14, 2000, 3:00:00 AM4/14/00
to
I'm reacting to following parts of "Steve Chapel" <sch...@breakthr.com>'s
article in comp.lang.java.help on Thu, 13 Apr 2000 12:20:11 -05006

. "Yun Mo" <mo...@crl.tel.co.jp> wrote in message
. news:38F5ACF7...@crl.tel.co.jp...
. > I appreciate your advice.
. >
. > Yes, I have done as what you said. The problem is that the length of
. formatted
. > number
. > changes when the number is less than 1 with the format as "0.000E00".
. > For an example,
. >
. > 0.01230 --> 1.230E-02 ( with the "0.000E00")
. >
. > In fact, I want the format: 1.230E-2 not 1.230E-02.
. >
. > No matter whether the number is less than 1 or not, I want to fix
. > the length of the number, just like in Fortran, C....
.
. Bug Id #4191595 is an RFE to allow using DecimalFormat to line up
. columns of numbers. If you want this capability in Java, sign up with
. the Java Developer Connection and vote for this RFE.


You could of course use FieldPosition for such 'fine-tuning'. But in my opinion
java.text.DecimalFormat is just too rude. I just succeeded to make my new
BigDecimalFormat class ready for beta-testing. First betatests will be reserved
to owners of the ArciMath BigDecimal production version.

Here is an extract from the javadoc (paste in into some empty HTML-document
yourself to view it):
-----------------------------------------------------------------------------------
<HTML><BODY>
<H3>BigDecimalFormat</H3>
BigDecimalFormat is an implementation of <tt>java.text.Format</tt>
that formats (large) decimal numbers. Because the many features of
BigDecimalFormat make it so much more advanced than the standard
<tt>java.text.DecimalFormat</tt> (we use the unqualified class name
<tt>DecimalFormat</tt> further on),
we decided to let BigDecimalFormat handle not only ArciMath BigDecimal numbers,

but also all of Java's primitive numeric types and all Number objects having a
decimal String representation, like there are <tt>java.math.BigInteger</tt>,
<tt>java.math.BigDecimal</tt>, <tt>com.ibm.math.BigDecimal</tt> and
<tt>com.tce.math.TBigDecimal</tt>. BigDecimalFormat uses intermediate ArciMath
BigDecimal objects for all arithmetic, rounding, and toString()
representations.
<P>
<H3>Treatment of overflow</H3>
BigDecimalFormat uses the ArciMath BigDecimal class for all arithmetic, which
offers a very strong protection against overflow both by the range of numbers
that can be represented as ArciMath BigDecimal objects, and by ArciMath's
watertight
arithmetic algorithms throwing an ArithMeticException on overflow.<BR>
Also BigDecimalFormat will never truncate most significand digits or the
exponent
of a formatted number; it gives you the choice to either extend the formatted
number
beyond the desired pattern width, or to throw an ArithMeticException instead.
<P>
<H3>Padding and alignment</H3>
For proper allignment of formatted numbers BigDecimalFormat supports replacing
non-significant zero digits with a padding character of choice, or padding
any number to a fixed width. The former kind of padding can only be specified
through the API method <tt>setZeroPaddingChar()</tt>.
The latter kind may be specified either through the API or through
the pattern syntax.
<P>
<H3>FieldPosition</H3>
If the padding alone is not sufficient for proper allignment (e.g. when using
proportional fonts), or if you need to
further customize the format result, you can use the FieldPosition argument to
the <tt>format()</tt> methods with one of the many xxx_FIELD constants of
BigDecimalFormat. After formatting, the FieldPosition object will contain the
beginning
and ending positions of the indicated field in the formatted number. If the
desired
field is not present in the formatted number, FieldPosition's begin and end
position
will be equal, and indicate the position where that field would have been. For
example,
with <tt>FieldPosition fp = new
FieldPosition(BigDecimalFormat.EXPONENT_SIGN_FIELD);</tt>
and a formatted number <tt>1.23E4</tt>, where there is no exponent sign shown,
<tt>fp.getBeginIndex()</tt> and <tt>fp.getEndIndex()</tt> both return 5,
because the
proper place for the exponent sign would have been at offset 5.
<P>
<H3>Rounding</H3>
On formatting BigDecimalFormat can have to round of a number to the specified
maximum number of decimal places. BigDecimalFormat allows to specify the
rounding
mode to use with that. The possible rounding modes are defined in MathContext,
and can only be specified through the API method <tt>setRoundingMode</tt>.
Rounding is done after an eventual multiplier is applied. Rounding only affects

the string produced by formatting. It does not affect parsing or change any
numerical values.
<P>
<H3>Multiples of a smallest module</H3>
BigDecimalFormat supports limiting a formatted number to
multiples of a specific smallest module. Examples for this are
the smallest coin for currency (5 pesetas in pre-Euro Spain), or
the smallest subdivision of sliding callipers, often 1/20 mm or 0.05mm.
The smallest module can also be used to round a formatted number to a
position within the integer field, commonly used to express a sum of
money in e.g. thousands or millions. The rounding mode set for the
BigDecimalFormat is used for formatting a number in multiples of the
smallest module.
<P>
The smallest module may be specified through the API or in a pattern.
To specify a smallest module in a pattern, include it's value as decimal
digits in the pattern itself. The first digit '1' through '9' starts the
specification of a smallest module, but otherwise behave identically to
digit '0'. In an exponential format, an exponent of 10 for the smallest modules

cannot be specified through the pattern. "#,##1,000" specifies a smallest
module
of 1000, "#,##0.25" specifies a smallest module of 0.25. If the smallest module

is 0.25, the number 12.30 may be rounded (according to the rounding mode in
use)
to 12.25 or 12.50.
<P>

<H3>Pattern Syntax</H3>
<tt><TABLE>
<TR><TH ALIGN=LEFT VALIGN=TOP> pattern </TH><TD
VALIGN=TOP>:=</TD> <TD VALIGN=TOP> posPattern { patternSep negPattern }
</TD></TR><TR><TH ALIGN=LEFT VALIGN=TOP> posPattern </TH><TD
VALIGN=TOP>:=</TD> <TD VALIGN=TOP> subPattern
</TD></TR><TR><TH ALIGN=LEFT VALIGN=TOP> negPattern </TH><TD
VALIGN=TOP>:=</TD> <TD VALIGN=TOP> subPattern
</TD></TR><TR><TH ALIGN=LEFT VALIGN=TOP> subPattern </TH><TD
VALIGN=TOP>:=</TD> <TD VALIGN=TOP> [ ( { paddedPrefix } number { suffix } ) |
<BR>

( { prefix } paddedNumber { suffix } ) | <BR>

( { prefix } number { paddedSuffix } ) ]
</TD></TR><TR><TH ALIGN=LEFT VALIGN=TOP> paddedPrefix </TH><TD
VALIGN=TOP>:=</TD> <TD VALIGN=TOP> paddedAffix
</TD></TR><TR><TH ALIGN=LEFT VALIGN=TOP> paddedSuffix </TH><TD
VALIGN=TOP>:=</TD> <TD VALIGN=TOP> paddedAffix
</TD></TR><TR><TH ALIGN=LEFT VALIGN=TOP> paddedAffix </TH><TD
VALIGN=TOP>:=</TD> <TD VALIGN=TOP> [ ( padSpec { affix } ) | ( affix padSpec ) ]
</TD></TR><TR><TH ALIGN=LEFT VALIGN=TOP> prefix </TH><TD
VALIGN=TOP>:=</TD> <TD VALIGN=TOP> affix
</TD></TR><TR><TH ALIGN=LEFT VALIGN=TOP> suffix </TH><TD
VALIGN=TOP>:=</TD> <TD VALIGN=TOP> affix
</TD></TR><TR><TH ALIGN=LEFT VALIGN=TOP> affix </TH><TD
VALIGN=TOP>:=</TD> <TD VALIGN=TOP> [ unquotedChar | quotedChars ]+
</TD></TR><TR><TH ALIGN=LEFT VALIGN=TOP> unquotedChar </TH><TD
VALIGN=TOP>:=</TD> <TD VALIGN=TOP> [ ( char - specialChar ) | expandedChar ]
</TD></TR><TR><TH ALIGN=LEFT VALIGN=TOP> char </TH><TD
VALIGN=TOP>:=</TD> <TD VALIGN=TOP> [ ( '&#92;u0000'..'&#92;uFFFD' - quote ) | (
quote quote ) ]
</TD></TR><TR><TH ALIGN=LEFT VALIGN=TOP> quotedChars </TH><TD
VALIGN=TOP>:=</TD> <TD VALIGN=TOP> quote char+ quote
</TD></TR><TR><TH ALIGN=LEFT VALIGN=TOP> expandedChar </TH><TD
VALIGN=TOP>:=</TD> <TD VALIGN=TOP> [ percent | perMille | currencySign | (
currencySign currencySign ) ]
</TD></TR><TR><TH ALIGN=LEFT VALIGN=TOP> specialChar </TH><TD
VALIGN=TOP>:=</TD> <TD VALIGN=TOP> [ expandedChar | patternSep | digitPlace |
digit |

decimalSep | groupingSep | padEscape ]
</TD></TR><TR><TH ALIGN=LEFT VALIGN=TOP> paddedNumber </TH><TD
VALIGN=TOP>:=</TD> <TD VALIGN=TOP> fixedPoint { padSpec } exponent
</TD></TR><TR><TH ALIGN=LEFT VALIGN=TOP> number </TH><TD
VALIGN=TOP>:=</TD> <TD VALIGN=TOP> fixedPoint exponent
</TD></TR><TR><TH ALIGN=LEFT VALIGN=TOP> fixedPoint </TH><TD
VALIGN=TOP>:=</TD> <TD VALIGN=TOP> [ integer | ( { integer } mantissa ) ]
</TD></TR><TR><TH ALIGN=LEFT VALIGN=TOP> mantissa </TH><TD
VALIGN=TOP>:=</TD> <TD VALIGN=TOP> { decimalSep { decimalPlaces } }
</TD></TR><TR><TH ALIGN=LEFT VALIGN=TOP> integer </TH><TD
VALIGN=TOP>:=</TD> <TD VALIGN=TOP> [ minIntDigits+ | ( maxIntDigits+ {
minIntDigits+ } ) ]
</TD></TR><TR><TH ALIGN=LEFT VALIGN=TOP> minIntDigits </TH><TD
VALIGN=TOP>:=</TD> <TD VALIGN=TOP> { groupingSep } digit+
</TD></TR><TR><TH ALIGN=LEFT VALIGN=TOP> maxIntDigits </TH><TD
VALIGN=TOP>:=</TD> <TD VALIGN=TOP> { groupingSep } digitPlace+
</TD></TR><TR><TH ALIGN=LEFT VALIGN=TOP> decimalplaces </TH><TD
VALIGN=TOP>:=</TD> <TD VALIGN=TOP> [ minDecPlaces+ | ( { minDecPlaces+ }
maxDecPlaces+ ) ]
</TD></TR><TR><TH ALIGN=LEFT VALIGN=TOP> minDecPlaces </TH><TD
VALIGN=TOP>:=</TD> <TD VALIGN=TOP> digit
</TD></TR><TR><TH ALIGN=LEFT VALIGN=TOP> maxDecPlaces </TH><TD
VALIGN=TOP>:=</TD> <TD VALIGN=TOP> digitPlace
</TD></TR><TR><TH ALIGN=LEFT VALIGN=TOP> exponent </TH><TD
VALIGN=TOP>:=</TD> <TD VALIGN=TOP> exponentSymbol { plusSign } exponentValue
</TD></TR><TR><TH ALIGN=LEFT VALIGN=TOP> exponentValue </TH><TD
VALIGN=TOP>:=</TD> <TD VALIGN=TOP> [ minExpDigits+ | ( { minExpDigits+ }
maxExpDigits+ ) ]
</TD></TR><TR><TH ALIGN=LEFT VALIGN=TOP> minExpDigits </TH><TD
VALIGN=TOP>:=</TD> <TD VALIGN=TOP> zeroDigit+
</TD></TR><TR><TH ALIGN=LEFT VALIGN=TOP> maxExpDigits </TH><TD
VALIGN=TOP>:=</TD> <TD VALIGN=TOP> digitPlace+
</TD></TR><TR><TH ALIGN=LEFT VALIGN=TOP> padSpec </TH><TD
VALIGN=TOP>:=</TD> <TD VALIGN=TOP> padEscape padChar
</TD></TR><TR><TH ALIGN=LEFT VALIGN=TOP> padChar </TH><TD
VALIGN=TOP>:=</TD> <TD VALIGN=TOP> char
</TD></TR><TR><TH ALIGN=LEFT VALIGN=TOP> patternSep </TH><TD
VALIGN=TOP>:=</TD> <TD VALIGN=TOP> [ ';' | localizedPatternSeparator ]
</TD></TR><TR><TH ALIGN=LEFT VALIGN=TOP> digitPlace </TH><TD
VALIGN=TOP>:=</TD> <TD VALIGN=TOP> [ '#' | localizedDigit ]
</TD></TR><TR><TH ALIGN=LEFT VALIGN=TOP> digit </TH><TD
VALIGN=TOP>:=</TD> <TD VALIGN=TOP> [ zeroDigit | nonZeroDigit ]
</TD></TR><TR><TH ALIGN=LEFT VALIGN=TOP> nonZeroDigit </TH><TD
VALIGN=TOP>:=</TD> <TD VALIGN=TOP> (zeroDigit + 1..9) <BR>

<I>This is an arithmetic sum!</I>
</TD></TR><TR><TH ALIGN=LEFT VALIGN=TOP> zeroDigit </TH><TD
VALIGN=TOP>:=</TD> <TD VALIGN=TOP> [ '0' | localizedZeroDigit ]
</TD></TR><TR><TH ALIGN=LEFT VALIGN=TOP> decimalSep </TH><TD
VALIGN=TOP>:=</TD> <TD VALIGN=TOP> [ '.' | localizedDecimalSeparator ]
</TD></TR><TR><TH ALIGN=LEFT VALIGN=TOP> groupingSep </TH><TD
VALIGN=TOP>:=</TD> <TD VALIGN=TOP> [ ',' | localizedGroupingSeparator ]
</TD></TR><TR><TH ALIGN=LEFT VALIGN=TOP> percent </TH><TD
VALIGN=TOP>:=</TD> <TD VALIGN=TOP> [ '%' | localizedPercent ]
</TD></TR><TR><TH ALIGN=LEFT VALIGN=TOP> perMille </TH><TD
VALIGN=TOP>:=</TD> <TD VALIGN=TOP> [ '&#137;' | localizedPerMill ] <BR>

<I>'&#137;' is Unicode character '&#92;u2030'</I>
</TD></TR><TR><TH ALIGN=LEFT VALIGN=TOP> currencySign </TH><TD
VALIGN=TOP>:=</TD> <TD VALIGN=TOP> '&#164;' <BR>

<I>'&#164;' is Unicode character '&#92;u00A4'</I>
</TD></TR><TR><TH ALIGN=LEFT VALIGN=TOP> exponentSymbol</TH><TD
VALIGN=TOP>:=</TD> <TD VALIGN=TOP> [ "E" | localizedExponentSymbol ]
</TD></TR><TR><TH ALIGN=LEFT VALIGN=TOP> padEscape </TH><TD
VALIGN=TOP>:=</TD> <TD VALIGN=TOP> '^'
</TD></TR><TR><TH ALIGN=LEFT VALIGN=TOP> plusSign </TH><TD
VALIGN=TOP>:=</TD> <TD VALIGN=TOP> '+'
</TD></TR><TR><TH ALIGN=LEFT VALIGN=TOP> quote </TH><TD
VALIGN=TOP>:=</TD> <TD VALIGN=TOP> '\'' <BR>

<I>The single quote character</I>
</TD></TR></TABLE></tt>
Notation:
<tt><TABLE>
<TR><TH ALIGN=LEFT VALIGN=TOP> * <TD VALIGN=TOP> 0
or more instances of
</TD></TR><TR><TH ALIGN=LEFT VALIGN=TOP> + <TD VALIGN=TOP> 1
or more instances of
</TD></TR><TR><TH ALIGN=LEFT VALIGN=TOP> ( ) <TD VALIGN=TOP>
groups several operands into 1 operand
</TD></TR><TR><TH ALIGN=LEFT VALIGN=TOP> { } <TD VALIGN=TOP> 0
or 1 instances of
</TD></TR><TR><TH ALIGN=LEFT VALIGN=TOP> [ X | Y | ... | Z ] <TD VALIGN=TOP> 1
of the choices X to Z
</TD></TR><TR><TH ALIGN=LEFT VALIGN=TOP> X..Y <TD VALIGN=TOP>
any character from X up to Y, inclusive
</TD></TR><TR><TH ALIGN=LEFT VALIGN=TOP> S - T <TD VALIGN=TOP>
characters in S, except those in T
</TD></TR></TABLE></tt><P>
<P>
<H4>Notes</H4>
<UL>
<LI>
<tt>DecimalFormat</tt> patterns should all be compatible with BigDecimalFormat,
though the reverse is certainly not true.
<LI>
The non-localized symbols are used with applyPattern() and toPattern().
The corresponding localized symbols are used with applyLocalizedPattern(),
toLocalizedPattern(), format() and parse(). They can be get/set through the
DecimalFormatSymbols object or (for the exponent symbol) through the
BigDecimalFormat object itself.
<LI>
The <tt>negPattern</tt> must be a valid <tt>subPattern</tt>, but all elements
except <tt>prefix</tt> and <tt>suffix</tt> are ignored.
<LI>
All <tt>groupingSep</tt> in an <tt>integer</tt> except the last one are
ignored.
<LI>
The first <tt>nonZeroDigit</tt> in the number pattern starts the specification
of the
smallest modulus.
<LI>
The <tt>plusSign</tt> in <tt>exponent</tt> makes format() to always print the
sign
of the exponent value; else only the sign for negative exponent values is
printed.
<LI>
On formatting non-significant zero digits are omitted at <tt>digitPlace</tt>
positions.
<LI>
On formatting non-significant zero digits are replaced by the zero padding
character,
if set, at <tt>zeroDigit</tt> positions.
<LI>
Patterns with only <tt>digitPlace</tt> symbols and no <tt>digit</tt> symbols
never format numbers without printing at least 1 digit.
<LI>
If <tt>exponentValue</tt> has only <tt>digitPlace</tt> symbols and no
<tt>digit</tt>
symbols, the exponent symbol and sign are omitted if the exponent value is
zero.
<LI>
An unquoted <tt>percent</tt> sets percent notation, with a multiplier of 100.
<LI>
An unquoted <tt>perMille</tt> sets per mille notation, with a multiplier of
1000.
<LI>
An unquoted <tt>currencySign</tt> sets national currency notation, where the
decimal
separator is replaced by the monetary decimal separator and the
<tt>currencySign</tt>
is expanded into the localized national currency symbol
<LI>
An unquoted <tt>( currencySign currencySign )</tt> sets international currency
notation,
where the decimal separator is replaced by the monetary decimal separator and
the
<tt>( currencySign currencySign )</tt> is expanded into the international
currency symbol.
<LI>
The <tt>exponentSymbol</tt> can appear unquoted in <tt>affix</tt>, unless when
a
<tt>suffix</tt> has to start with what can be parsed as a valid
<tt>exponent</tt>
specification.
</UL>
</BODY></HTML>
---------------------------------------------------------------------------------------

Greetings,
Dirk Bosmans

- Applicet Framework: turns Applets into Applications
- ArciMath BigDecimal
(main http://users.belgacombusiness.net/arci/).
(mirror http://www.freeyellow.com/members7/db-inf)
(obsolete http://www.ping.be/arci/applet)
(obsolete http://www.ping.be/arci/math)

Yun Mo

unread,
Apr 17, 2000, 3:00:00 AM4/17/00
to Dirk Bosmans
Dear Dirk bosmans,

I very appreciate your help. With your help, I found a solution from IBM site
with "BigDecimalFormat".

best regards,

M.Y., TDC/TEL

Dirk Bosmans wrote:

> I'm reacting to following parts of "Steve Chapel" <sch...@breakthr.com>'s
> article in comp.lang.java.help on Thu, 13 Apr 2000 12:20:11 -05006
>
> . "Yun Mo" <mo...@crl.tel.co.jp> wrote in message
> . news:38F5ACF7...@crl.tel.co.jp...
> . > I appreciate your advice.
> . >
> . > Yes, I have done as what you said. The problem is that the length of
> . formatted
> . > number
> . > changes when the number is less than 1 with the format as "0.000E00".
> . > For an example,
> . >
> . > 0.01230 --> 1.230E-02 ( with the "0.000E00")
> . >
> . > In fact, I want the format: 1.230E-2 not 1.230E-02.
>
>

Yun Mo

unread,
Apr 17, 2000, 3:00:00 AM4/17/00
to Dirk Bosmans
I am sorry to disturb you again.

With the IBM BigDecimal class, I could format a number with fixed length.

It still remains a question. I failed to express 0.5 as 5.000E-01.

Even with BigDecinal(0.5).format(2,3, 2,0, 2, -1), I got " 0.500 " not " 5.000E-01".

Do you have any method to help me again?

Thank you very much.

best regards,

M.Y. TDC/TEL

Dirk Bosmans

unread,
Apr 17, 2000, 3:00:00 AM4/17/00
to
That is why I referred to the new BigDecimalFormat class that is in test now.
BigDecimalFormat can do what you are looking for, but is now in beta test for
owners of a license to the ArciMath BigDecimal production version.

I'm reacting to following parts of Yun Mo <mo...@crl.tel.co.jp>'s article in
comp.lang.java.help on Mon, 17 Apr 2000 12:50:10 +09006

. I am sorry to disturb you again.
.
. With the IBM BigDecimal class, I could format a number with fixed length.
.
. It still remains a question. I failed to express 0.5 as 5.000E-01.
.
. Even with BigDecinal(0.5).format(2,3, 2,0, 2, -1), I got " 0.500 " not "
5.000E-01".
.

. Do you have any method to help me again?

.
. Thank you very much.
.
. best regards,
.
. M.Y. TDC/TEL
.

Yun Mo

unread,
Apr 17, 2000, 3:00:00 AM4/17/00
to
For the "E" format, I tried with extending java.text.DecimalFormat
as in the sample code attached. The method is

myformat(double a, DecimalFormat df)
myformat(int i, DecimalFormat df)


It works for the fixed length format.
However, it also get wrong result for 1.005 with "0.00E00".
The expected result is 1.01E+00 but result is 1.00E+00.
This results from java.text.DecimalFormat.


M.Y., TDC/TEL

---------------------------------------

import java.text.DecimalFormat;

public class mytest
{
public static void main(String args[])
{
DecimalFormat df = new DecimalFormat("0.00E00");
// In fact, the total length of this format should include 2 extra
digits for the plus/minus sign
// like "+0.00E+00".


double a;


a=1.0e-2;
System.out.println("a="+a+" formatted="+myformat(a,df));
a=-1.006e5;
System.out.println("a="+a+" formatted="+myformat(a,df));

// The formatted result is wrong for 1.00t0e5
a=-1.0050e5;
System.out.println("a="+a+" formatted="+myformat(a,df));

a = 0.0;
System.out.println("a="+a+" formatted="+myformat(a,df));


a =-1230.0;
System.out.println("a="+a+" formatted="+myformat(a,df));


int b = 100000;
System.out.println("b="+b+" formatted="+myformat(b,df));


}

public static String myformat(double a, DecimalFormat df) {
String s = df.format(a);
//System.out.println("a="+ a + " Formated :"+ s);


if(a>0) s = "+" + s;
else if (a==0) s = " " + s;
//System.out.println("a="+ a + " Formated :"+ s);

int i;
i=s.lastIndexOf('E');
//System.out.println("index of E "+ i);

String s1,s2;
s1 = s.substring(0,i);
//System.out.println("s1 =:"+s1);

s2=s.substring(i+1);
//System.out.println("s2="+s2);

if(Integer.parseInt(s2)>=0 ) s2 = "+" + s2;
//System.out.println("Modified s2="+s2);
//System.out.println("a ="+a + " Formatted ="+s1+"E"+s2);
return s1 + "E" + s2;
}

public static String myformat(int i, DecimalFormat df) {
return myformat((double)i, df);
}


}


Patricia Shanahan

unread,
Apr 17, 2000, 3:00:00 AM4/17/00
to
First of all, 1.005 is not exactly representable as a double. You will
actually get a number slightly larger or smaller.

However, suppose the case were 100.5, which is representable in double.
The correct answer would be 100, not 101, because "Numbers are rounded
toward the nearest truncated value, unless both truncated values are
equidistant, in which case the value ending in an even digit is
chosen.".

If numbers that happen to be exactly representable in decimal are
particularly important in your program use BigDecimal instead of double.
Even if double is right for your general arithmetic, if you want a
different rounding mode from round half even convert to BigDecimal
before display, use BigDecimal to do the formatting.

Patricia

Dirk Bosmans

unread,
Apr 17, 2000, 3:00:00 AM4/17/00
to
And don't use java.text.DecimalFormat with exponent notations. Try this one:

java.text.DecimalFormat df = new java.text.DecimalFormat("##0.###E0");
System.out.println(df.format(99999.99951));//should be 100E3, gives 1E3

Dangerous. And what about fixed point? Do you like loosing most-significand
digits without a warning?

java.text.DecimalFormat df =
(java.text.DecimalFormat)java.text.NumberFormat.getInstance();
df.setMaximumIntegerDigits(3);
System.out.println(df.format(99999.99951));//should at least throw an
//exception, gives 000

I'm reacting to following parts of Patricia Shanahan <pa...@acm.org>'s article in
comp.lang.java.help on 17 Apr 2000 09:31:08 EDT6

....

. If numbers that happen to be exactly representable in decimal are

. particularly important in your program use BigDecimal instead of double.
. Even if double is right for your general arithmetic, if you want a
. different rounding mode from round half even convert to BigDecimal
. before display, use BigDecimal to do the formatting.
.
. Patricia


Greetings,
Dirk Bosmans


http://users.belgacombusiness.net/arci/


- Applicet Framework: turns Applets into Applications

- ArciMath BigDecimal: now with BigDecimalFormat

Yun Mo

unread,
Apr 18, 2000, 3:00:00 AM4/18/00
to
Thank you for your suggestion.

With the other languages, such as fortran, I also got the same result with the
same format.
Follows is a test code with Fortran.


a=1.005
write(*,10) a,a,a,a
10 format(' a= ', E10.2, 2x, E11.3, 2x, E12.4, 2x, E13.5)
end

I got:
a= 0.10E+01 0.100E+01 0.1005E+01 0.10050E+01

At this point, I have to abandon the idea for the accuracy of the last digit.
It is originally uncertain.


M.Y.

Patricia Shanahan wrote:

> First of all, 1.005 is not exactly representable as a double. You will
> actually get a number slightly larger or smaller.
>
> However, suppose the case were 100.5, which is representable in double.
> The correct answer would be 100, not 101, because "Numbers are rounded
> toward the nearest truncated value, unless both truncated values are
> equidistant, in which case the value ending in an even digit is

> chosen.".


>
> If numbers that happen to be exactly representable in decimal are

> particularly important in your program use BigDecimal instead of double.

> Even if double is right for your general arithmetic, if you want a

> different rounding mode from round half even convert to BigDecimal

> before display, use BigDecimal to do the formatting.
>

> Patricia
>


Yun Mo

unread,
Apr 18, 2000, 3:00:00 AM4/18/00
to
With the format "##0.##E00" and method of myformat, I got:

a=0.01 formatted=+1E-03
a=-100600.0 formatted=-100.6E+03
a=-100500.0 formatted=-100.5E+03
a=-100510.0 formatted=-100.51E+03
a=-100500.0000000001 formatted=-100.5E+03
a=0.0 formatted= 0E+00
a=-1230.0 formatted=-1.23E+03
a=99999.99951 formatted=+1E+03
b=100000 formatted=+1E+03

With "0.00E00" and the method of myformat, I obtained:

a=0.01 formatted=+1.00E-02
a=-100600.0 formatted=-1.01E+05
a=-100500.0 formatted=-1.00E+05 (x)
a=-100510.0 formatted=-1.01E+05 (ok)
a=-100500.0000000001 formatted=-1.01E+05 (ok)
a=0.0 formatted= 0.00E+00
a=-1230.0 formatted=-1.23E+03
a=99999.99951 formatted=+1.00E+05
b=100000 formatted=+1.00E+05


It seems that no problem for the fixed decimal format like "0.00E00".
Of course, there exists a worry about formatting 100500 with "0.00E00".
In engineering, we only want some number of valid digits.

The following format is a despoit result. The reason may be that it
takes digit from right side of 100,000.

At lease, the maximum integer digits should larger than 6.
I dont't know the exact reason that it could get the expected result with "0.00E00".

But it can guess that it takes digits from left side of 100,00 for "0.00E00".

> java.text.DecimalFormat df =
> (java.text.DecimalFormat)java.text.NumberFormat.getInstance();
> df.setMaximumIntegerDigits(3);
> System.out.println(df.format(99999.99951));//should at least throw an
//exception, gives 000


Dirk Bosmans wrote:

> And don't use java.text.DecimalFormat with exponent notations. Try this one:
>
> java.text.DecimalFormat df = new java.text.DecimalFormat("##0.###E0");
> System.out.println(df.format(99999.99951));//should be 100E3, gives 1E3
>
> Dangerous. And what about fixed point? Do you like loosing most-significand
> digits without a warning?
>
> java.text.DecimalFormat df =
> (java.text.DecimalFormat)java.text.NumberFormat.getInstance();
> df.setMaximumIntegerDigits(3);
> System.out.println(df.format(99999.99951));//should at least throw an
> //exception, gives 000
>
> I'm reacting to following parts of Patricia Shanahan <pa...@acm.org>'s article in
> comp.lang.java.help on 17 Apr 2000 09:31:08 EDT6
>
> ....

> . If numbers that happen to be exactly representable in decimal are

> . particularly important in your program use BigDecimal instead of double.
> . Even if double is right for your general arithmetic, if you want a
> . different rounding mode from round half even convert to BigDecimal
> . before display, use BigDecimal to do the formatting.
> .
> . Patricia
>
> Greetings,
> Dirk Bosmans
>
> http://users.belgacombusiness.net/arci/

> - Applicet Framework: turns Applets into Applications

> - ArciMath BigDecimal: now with BigDecimalFormat


Little Ninja

unread,
Nov 18, 2020, 8:35:59 PM11/18/20
to
> > I'm reacting to following parts of Patricia Shanahan <pa...@acm.org>'s article in
> > comp.lang.java.help on 17 Apr 2000 09:31:08 EDT6
> >
> > ....
> > . If numbers that happen to be exactly representable in decimal are
> > . particularly important in your program use BigDecimal instead of double.
> > . Even if double is right for your general arithmetic, if you want a
> > . different rounding mode from round half even convert to BigDecimal
> > . before display, use BigDecimal to do the formatting.
> > .
> > . Patricia
> >
> > Greetings,
> > Dirk Bosmans
> >
> > http://users.belgacombusiness.net/arci/
> > - Applicet Framework: turns Applets into Applications
> > - ArciMath BigDecimal: now with BigDecimalFormat
u a bot and a loser
Reply all
Reply to author
Forward
0 new messages