< prev index next >

src/java.base/share/classes/java/text/NumberFormat.java

Print this page
rev 47480 : [mq]: 8176841


  79  * </blockquote>
  80  * If you are formatting multiple numbers, it is
  81  * more efficient to get the format and use it multiple times so that
  82  * the system doesn't have to fetch the information about the local
  83  * language and country conventions multiple times.
  84  * <blockquote>
  85  * <pre>{@code
  86  * NumberFormat nf = NumberFormat.getInstance();
  87  * for (int i = 0; i < myNumber.length; ++i) {
  88  *     output.println(nf.format(myNumber[i]) + "; ");
  89  * }
  90  * }</pre>
  91  * </blockquote>
  92  * To format a number for a different Locale, specify it in the
  93  * call to <code>getInstance</code>.
  94  * <blockquote>
  95  * <pre>{@code
  96  * NumberFormat nf = NumberFormat.getInstance(Locale.FRENCH);
  97  * }</pre>
  98  * </blockquote>
  99  * You can also use a <code>NumberFormat</code> to parse numbers:







 100  * <blockquote>
 101  * <pre>{@code
 102  * myNumber = nf.parse(myString);
 103  * }</pre>
 104  * </blockquote>
 105  * Use <code>getInstance</code> or <code>getNumberInstance</code> to get the
 106  * normal number format. Use <code>getIntegerInstance</code> to get an
 107  * integer number format. Use <code>getCurrencyInstance</code> to get the
 108  * currency number format. And use <code>getPercentInstance</code> to get a
 109  * format for displaying percentages. With this format, a fraction like
 110  * 0.53 is displayed as 53%.
 111  *
 112  * <p>
 113  * You can also control the display of numbers with such methods as
 114  * <code>setMinimumFractionDigits</code>.
 115  * If you want even more control over the format or parsing,
 116  * or want to give your users more control,
 117  * you can try casting the <code>NumberFormat</code> you get from the factory methods
 118  * to a <code>DecimalFormat</code>. This will work for the vast majority
 119  * of locales; just remember to put it in a <code>try</code> block in case you




  79  * </blockquote>
  80  * If you are formatting multiple numbers, it is
  81  * more efficient to get the format and use it multiple times so that
  82  * the system doesn't have to fetch the information about the local
  83  * language and country conventions multiple times.
  84  * <blockquote>
  85  * <pre>{@code
  86  * NumberFormat nf = NumberFormat.getInstance();
  87  * for (int i = 0; i < myNumber.length; ++i) {
  88  *     output.println(nf.format(myNumber[i]) + "; ");
  89  * }
  90  * }</pre>
  91  * </blockquote>
  92  * To format a number for a different Locale, specify it in the
  93  * call to <code>getInstance</code>.
  94  * <blockquote>
  95  * <pre>{@code
  96  * NumberFormat nf = NumberFormat.getInstance(Locale.FRENCH);
  97  * }</pre>
  98  * </blockquote>
  99  *
 100  * <p>If the locale contains "nu" (numbers) and/or "rg" (region override)
 101  * <a href="../util/Locale.html#def_locale_extension">Unicode extensions</a>,
 102  * the decimal digits, and/or the country used for formatting are overriden.
 103  * If both "nu" and "rg" are specified, the decimal digits from "nu"
 104  * extension supersedes the implicit one from "rg" extension.
 105  *
 106  * <p>You can also use a <code>NumberFormat</code> to parse numbers:
 107  * <blockquote>
 108  * <pre>{@code
 109  * myNumber = nf.parse(myString);
 110  * }</pre>
 111  * </blockquote>
 112  * Use <code>getInstance</code> or <code>getNumberInstance</code> to get the
 113  * normal number format. Use <code>getIntegerInstance</code> to get an
 114  * integer number format. Use <code>getCurrencyInstance</code> to get the
 115  * currency number format. And use <code>getPercentInstance</code> to get a
 116  * format for displaying percentages. With this format, a fraction like
 117  * 0.53 is displayed as 53%.
 118  *
 119  * <p>
 120  * You can also control the display of numbers with such methods as
 121  * <code>setMinimumFractionDigits</code>.
 122  * If you want even more control over the format or parsing,
 123  * or want to give your users more control,
 124  * you can try casting the <code>NumberFormat</code> you get from the factory methods
 125  * to a <code>DecimalFormat</code>. This will work for the vast majority
 126  * of locales; just remember to put it in a <code>try</code> block in case you


< prev index next >