Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
428 views
in Technique[技术] by (71.8m points)

java - DecimalFormat pattern

public static String formatAmountUpToTwoDecimalNumber(String amount)
    {       
        if(amount==null || "".equals(amount))
        {
             return "";
        }  
        Double doubleAmount = Double.valueOf(amount);
        double myAmount = doubleAmount.doubleValue();
        NumberFormat f = new DecimalFormat("###,###,###,###,##0.00");
        String s = f.format(myAmount);
        return s;
    }

"###,###,###,###,##0.00", What exactly is the purpose of this pattern ? I believe it serves two purposes

  1. to group numbers, that is put thousand seperator comma
  2. to append two zeros after decimal if decimal is missing that is convert 23 to 23.00

But why there is "0" instead of "#" before decimal? what exactly is the purpose of this zero? Thanks for the help.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
Symbol  Location    Localized?  Meaning
0       Number      Yes         Digit
#       Number      Yes         Digit, zero shows as absent 

From: http://docs.oracle.com/javase/7/docs/api/java/text/DecimalFormat.html

So # is not shown when there is no number. The leading 0 means there will be at least 1 digit before the decimal separator.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...