Java Tutorial/I18N/NumberFormat

Материал из Java эксперт
Версия от 05:03, 1 июня 2010; Admin (обсуждение | вклад) (1 версия)
(разн.) ← Предыдущая | Текущая версия (разн.) | Следующая → (разн.)
Перейти к: навигация, поиск

Format a number for a locale

import java.text.NumberFormat;
import java.util.Locale;
public class Main {
  public static void main(String[] args) throws Exception {
    NumberFormat formatter = NumberFormat.getNumberInstance(Locale.ITALY);
    String number = formatter.format(123456789.12);
    System.out.println("Number in Italy: " + number);
    formatter = NumberFormat.getNumberInstance(Locale.JAPAN);
    number = formatter.format(123456789.12);
    System.out.println("Number in Japan: " + number);
  }
}





Format a number with DecimalFormat

import java.text.DecimalFormat;
import java.text.NumberFormat;
public class Main {
  public static void main(String[] args) {
    double money = 123456789.12;
    NumberFormat formatter = new DecimalFormat("#0.00");
    System.out.println(money);
    System.out.println(formatter.format(money));
  }
}





Format for GERMAN locale

import java.text.NumberFormat;
import java.util.Locale;
public class Main {
  public static void main(String[] argv) throws Exception {
    Locale locale = Locale.GERMAN;
    String string = NumberFormat.getNumberInstance(locale).format(-1234.56); 
    System.out.println(string);
  }
}
//-1.234,56





Format for the default locale

import java.text.NumberFormat;
public class Main {
  public static void main(String[] argv) throws Exception {
    String string = NumberFormat.getNumberInstance().format(-1234.56);
    System.out.println(string);
  }
}
//-1,234.56





Formatting and Parsing a Locale-Specific Percentage

import java.text.NumberFormat;
import java.util.Locale;
public class Main {
  public static void main(String[] argv) throws Exception {
    // Format
    Locale locale = Locale.CANADA;
    String string = NumberFormat.getPercentInstance(locale).format(123.45);
  }
}
// 12,345%





Formatting and Parsing Locale-Specific Currency

import java.text.NumberFormat;
import java.util.Locale;
public class Main {
  public static void main(String[] argv) throws Exception {
    // Format
    Locale locale = Locale.GERMANY;
    String string = NumberFormat.getCurrencyInstance(locale).format(123.45);
  }
}
// 123,45 DM





Formatting a Number in Exponential Notation

The "E" symbol specifies that a number should be formatted in exponential notation. 
E symbol separates the mantissa from the exponent. 
E symbol must be followed by one or more "0" symbols. 
The number of "0" symbols specifies the minimum number of digits used to display the exponent.





Parse a GERMAN number

import java.text.NumberFormat;
import java.util.Locale;
public class Main {
  public static void main(String[] argv) throws Exception {
    Number number = NumberFormat.getNumberInstance(Locale.GERMAN).parse("-1.234,56");
    if (number instanceof Long) {
      System.out.println("Long value"); 
    } else {
      System.out.println("Double value"); 
    }
  }
}





Parse a number for a locale

import java.text.NumberFormat;
import java.util.Locale;
public class Main {
  public static void main(String[] args) throws Exception {
    Number number = NumberFormat.getNumberInstance(Locale.JAPAN).parse("25,000.75");
    if (number instanceof Long) {
      System.out.println("Long value: " + number.longValue());
    } else if (number instanceof Double) {
      System.out.println("Double value: " + number.doubleValue());
    }
  }
}





Parse a number with NumberFormat and Locale.CANADA

import java.text.NumberFormat;
import java.util.Locale;
public class Main {
  public static void main(String[] argv) throws Exception {
    Number number = NumberFormat.getPercentInstance(Locale.CANADA).parse("123.45%");
    // 1.2345
    if (number instanceof Long) {
      System.out.println("Long value");
    } else {
      System.out.println("Double value");
    }
  }
}





Parse number with NumberFormat and Locale

import java.text.NumberFormat;
import java.util.Locale;
public class Main {
  public static void main(String[] argv) throws Exception {
    Number number = NumberFormat.getCurrencyInstance(Locale.GERMANY).parse("$123.45");
    if (number instanceof Long) {
      System.out.println("Long value");
    } else {
      System.out.println("Double value");
    }
  }
}





Set format to two decimal places

import java.text.NumberFormat;
public class Main {
  public static void main(String args[]) {
    NumberFormat nf = NumberFormat.getInstance();
    System.out.println("Default format: " + nf.format(1234567.678));
    nf.setMaximumFractionDigits(2);
    System.out.println("Format with two decimal places: " + nf.format(1234567.678));
  }
}
/*
Default format: 1,234,567.678
Format with two decimal places: 1,234,567.68
*/





Use grouping to display a number

import java.text.NumberFormat;
public class Main {
  public static void main(String args[]) {
    NumberFormat nf = NumberFormat.getInstance();
    System.out.println("Default format: " + nf.format(1234567.678));
    nf.setGroupingUsed(false);
    System.out.println("Format without groupings: " + nf.format(1234567.678));
  }
}
/*
Default format: 1,234,567.678
Format without groupings: 1234567.678
*/





Use java.text.NumberFormat to format a currency value.

import java.text.NumberFormat;
public class Main {
  public static void main(String[] argv) throws Exception {
    NumberFormat nf = NumberFormat.getCurrencyInstance();
    System.out.println("currency format: " + nf.format(1234.56) + " "
        + nf.format(-1234.56));
  }
}
//currency format: $1,234.56 -$1,234.56





Using only 0"s to the left of E forces no decimal point

import java.text.DecimalFormat;
import java.text.NumberFormat;
public class Main {
  public static void main(String[] argv) throws Exception {
    NumberFormat formatter = new DecimalFormat("0E0");
    String s = formatter.format(-1234.567);
    System.out.println(s);
  }
}
// -1E3