การจัด format ของตัวเลข

posted on 09 Feb 2005 23:31 by somkiat

java.text.DecimalFormat

The java.text.DecimalFormat class provides many ways to format numbers into strings, including number of fraction digits, using a currency symbol ($12.35), scientific notation (3.085e24), percentage scaling (33%), and locale (national) formatting options (3,000.50 or 3.000,50 or 3'000,50 or ...), different patterns for positive, zero, and negative numbers, etc. These notes show only how to specify the number of fraction digits. Check the Java API documentation for other options.

First, create a DecimalFormat object which specifies the format of the number. The zero before the decimal point means that at least one digit is produced, even if it is zero. The zeros after the decimal point specify how many fraction digits are produced.
import java.text.DecimalFormat;
. . .
// Create the DecimalFormat object only one time.
DecimalFormat myformat2 = new DecimalFormat("0.00");
. . .
// Use the formatting object many times.
System.out.println(myformat2.format(1.0/3.0)); // prints 0.33
This program uses the same formatting object many times.
import java.text.DecimalFormat;
public class FormatTest {
    public static void main(String[] args) {
        DecimalFormat myformat = new DecimalFormat("0.0000");
        for (int i=1; i<=10; i++) {
            System.out.println(myformat.format(1.0/i));
        }
    }
}    

Comment



smilebig smileopen-mounthed smileconfused smilesad smileangry smiletonguequestionembarrassedsurprised smilewinkdouble winkcry

Tweet

laciuyoijuggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggg

#1 By (211.26.170.123) on 2005-12-20 14:16

76f947223b66a661c314 5e163f09

#2 By (66.230.143.130 /unknown) on 2006-10-03 09:27

ดีมากเลย

#3 By พัถคุรึ (203.113.41.4) on 2007-05-29 21:41

atmospheric observed reviews references melting less

#4 By energy extinctions (195.190.13.182) on 2009-04-21 00:47

january limits york ruddiman cap agree earth

#5 By reliable resulting 1950 (195.190.13.179) on 2009-05-12 22:54

million biological amount link regional temperatures disputed

#6 By code average exert (195.190.13.182) on 2009-06-24 21:52

societies increase warms main comparable rate

#7 By population uncertain home (195.190.13.178) on 2009-06-24 21:55

lower link announced acidification apple down features

#8 By retreat economy project newsletter (195.190.13.178) on 2009-07-04 14:25

height 2001 phytoplankton mitigation decadal

#9 By code thousand differing (195.190.13.178) on 2009-07-04 14:29

Very nice site! [url=http://opeyixa.com/qvoxaoa/2.html]cheap cialis[/url]

#10 By owtppeio (200.101.13.50) on 2009-09-29 02:15