Results 1 to 5 of 5

Thread: How to format a number in java?

  1. #1
    Join Date
    Aug 2009
    Posts
    63

    How to format a number in java?

    Hi friends,
    I am new to this forum. I am last year B.Sc.I.T. I am working on project where I use java as front end. I want to display number in particular format. Can anyone tell me How to format a number in java? Please help me to find answer. Thank you.

  2. #2
    Join Date
    Apr 2008
    Posts
    1,948

    Re: How to format a number in java?

    You can format a number in java in following ways. If you want to display some numbers that is formatted to a certain pattern in your project then you can utilize NumberFormat and DecimalFormat class to give you the format that you want. Here I had written example that will show you how to do it.

    Code:
    package org.kodejava.example.text;
     
    import java.text.DecimalFormat;
    import java.text.NumberFormat;
     
    public class DecimalFormat
    {
        public static void main(String[] args)
        {
            NumberFormat F1 = new DecimalFormat("#0.00");
            System.out.println(M);
           
            System.out.println(F1.format(M));
        }
    }

  3. #3
    Join Date
    May 2008
    Posts
    2,012

    Re: How to format a number in java?

    You have to use pattern of special characters to specify the format of the number. Following example demonstrates some of the characters.

    Code:
    // The 0 symbol shows a digit or 0 if no digit present 
    NumberFormat F1 = new DecimalFormat("000000"); 
    String S = F1.format(-4321.567); // -004321
    Code:
    // The # symbol shows a digit or nothing if no digit present 
    F1 = new DecimalFormat("##"); 
    S = F1.format(-4321.567); // -4321 
    S = F1.format(0); // 0 
    F1 = new DecimalFormat("##00");
    S = F1.format(0); // 00

  4. #4
    Join Date
    Apr 2008
    Posts
    2,005

    Re: How to format a number in java?

    You can format number in following ways.


    Code:
    import java.text.NumberFormat;
    import java.util.Locale;
    
    public class NFD {
    
      static public void displayNumber(Locale currentLocale) {
    
        Integer q = new Integer(65231);
        Double a = new Double(58741.246);
        NumberFormat nFormatter;
        String qOut;
        String aOut;
    
        nFormatter = NumberFormat.getNumberInstance(cLocale);
        qOut = numberFormatter.format(q);
        aOut = numberFormatter.format(amount);
        System.out.println(qOut + "   " + cLocale.toString());
        System.out.println(aOut + "   " + cLocale.toString());
      }
    
      static public void displayCurrency(Locale currentLocale) {
    
        Double c = new Double(3652159.21);
        NumberFormat cFormatter;
        String cOut;
    
        cFormatter = NumberFormat.getCurrencyInstance(cLocale);
        cOut = currencyFormatter.format(c);
        System.out.println(currencyOut + "   " + cLocale.toString());
      }
    
      static public void displayPercent(Locale cLocale) {
    
        Double p = new Double(0.75);
        NumberFormat pFormatter;
        String pOut;
    
        pFormatter = NumberFormat.getPInstance(currentLocale);
        pOut = pFormatter.format(p);
        System.out.println(pOut + "   " + cLocale.toString());
      }
    
      static public void main(String[] args) {
    
        Locale[] locales = { new Locale("fr", "FR"), new Locale("de", "DE"),
            new Locale("en", "US") };
    
        for (int h = 0; h < locales.length; h++) {
          System.out.println();
          displayNumber(locales[h]);
          displayCurrency(locales[h]);
          displayPercent(locales[h]);
        }
      }
    
    }

  5. #5
    Join Date
    Apr 2008
    Posts
    2,005

    Re: How to format a number in java?

    Hey there are different way to format number. I had written 3 example to do this. Just go through it.


    Code:
    double p = 2.6854;
    System.out.println(p); // p is 2.6854
    
    int q = 3;
    BigDecimal d = new BigDecimal(p);
    
    // setScale is immutable
    d = d.setScale(decimalPlaces, BigDecimal.ROUND_HALF_UP);
    p = d.doubleValue();
    
    System.out.println(p); // p is 2.68
    Code:
    d = (float) (Math.round(n*1200.0f)/200.0f);
    Code:
    DecimalFormat cms = new DecimalFormat( "#,###,###,##0.00" );
    double fg = 100.2397;
    double fg2erd = new Double(dfg.format(dfg)).doubleValue();
    
    // The value of fg2erddec will be 200.23

Similar Threads

  1. Java : test a number to find out if its is even or odd
    By teenQ in forum Software Development
    Replies: 4
    Last Post: 06-04-2012, 06:42 AM
  2. Determine a whole number in java
    By Remedy in forum Software Development
    Replies: 5
    Last Post: 25-02-2010, 04:39 AM
  3. how to find median of given number in java?
    By Aloke in forum Software Development
    Replies: 3
    Last Post: 26-11-2009, 09:27 PM
  4. Random number generation in java
    By GlassFish in forum Software Development
    Replies: 3
    Last Post: 16-11-2009, 11:42 AM
  5. How to convert vb .text to number format
    By Brunoz in forum Software Development
    Replies: 6
    Last Post: 04-07-2009, 09:18 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,714,836,673.88239 seconds with 17 queries