1 /*
2 * Copyright (c) 1999, 2012, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation. Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
156 * @param locale the desired locale
157 * @exception NullPointerException if <code>locale</code> is null
158 * @exception IllegalArgumentException if <code>locale</code> isn't
159 * one of the locales returned from
160 * {@link java.util.spi.LocaleServiceProvider#getAvailableLocales()
161 * getAvailableLocales()}.
162 * @return a percent formatter
163 * @see java.text.NumberFormat#getPercentInstance(java.util.Locale)
164 */
165 @Override
166 public NumberFormat getPercentInstance(Locale locale) {
167 return getInstance(locale, PERCENTSTYLE);
168 }
169
170 private NumberFormat getInstance(Locale locale,
171 int choice) {
172 if (locale == null) {
173 throw new NullPointerException();
174 }
175
176 LocaleProviderAdapter adapter = LocaleProviderAdapter.forType(type);
177 String[] numberPatterns = adapter.getLocaleResources(locale).getNumberPatterns();
178 DecimalFormatSymbols symbols = DecimalFormatSymbols.getInstance(locale);
179 int entry = (choice == INTEGERSTYLE) ? NUMBERSTYLE : choice;
180 DecimalFormat format = new DecimalFormat(numberPatterns[entry], symbols);
181
182 if (choice == INTEGERSTYLE) {
183 format.setMaximumFractionDigits(0);
184 format.setDecimalSeparatorAlwaysShown(false);
185 format.setParseIntegerOnly(true);
186 } else if (choice == CURRENCYSTYLE) {
187 adjustForCurrencyDefaultFractionDigits(format, symbols);
188 }
189
190 return format;
191 }
192
193 /**
194 * Adjusts the minimum and maximum fraction digits to values that
195 * are reasonable for the currency's default fraction digits.
196 */
197 private static void adjustForCurrencyDefaultFractionDigits(
198 DecimalFormat format, DecimalFormatSymbols symbols) {
|
1 /*
2 * Copyright (c) 1999, 2017, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation. Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
156 * @param locale the desired locale
157 * @exception NullPointerException if <code>locale</code> is null
158 * @exception IllegalArgumentException if <code>locale</code> isn't
159 * one of the locales returned from
160 * {@link java.util.spi.LocaleServiceProvider#getAvailableLocales()
161 * getAvailableLocales()}.
162 * @return a percent formatter
163 * @see java.text.NumberFormat#getPercentInstance(java.util.Locale)
164 */
165 @Override
166 public NumberFormat getPercentInstance(Locale locale) {
167 return getInstance(locale, PERCENTSTYLE);
168 }
169
170 private NumberFormat getInstance(Locale locale,
171 int choice) {
172 if (locale == null) {
173 throw new NullPointerException();
174 }
175
176 // Check for region override
177 Locale override = locale.getUnicodeLocaleType("nu") == null ?
178 CalendarDataUtility.findRegionOverride(locale).orElse(locale) :
179 locale;
180
181 LocaleProviderAdapter adapter = LocaleProviderAdapter.forType(type);
182 String[] numberPatterns = adapter.getLocaleResources(override).getNumberPatterns();
183 DecimalFormatSymbols symbols = DecimalFormatSymbols.getInstance(override);
184 int entry = (choice == INTEGERSTYLE) ? NUMBERSTYLE : choice;
185 DecimalFormat format = new DecimalFormat(numberPatterns[entry], symbols);
186
187 if (choice == INTEGERSTYLE) {
188 format.setMaximumFractionDigits(0);
189 format.setDecimalSeparatorAlwaysShown(false);
190 format.setParseIntegerOnly(true);
191 } else if (choice == CURRENCYSTYLE) {
192 adjustForCurrencyDefaultFractionDigits(format, symbols);
193 }
194
195 return format;
196 }
197
198 /**
199 * Adjusts the minimum and maximum fraction digits to values that
200 * are reasonable for the currency's default fraction digits.
201 */
202 private static void adjustForCurrencyDefaultFractionDigits(
203 DecimalFormat format, DecimalFormatSymbols symbols) {
|