Beispiel #1
0
  public void test(TestHarness harness) {
    Currency currency;

    /* Set default Locale for the JVM */
    Locale.setDefault(TEST_LOCALE);
    /* Get an instance of the currency */
    currency = Currency.getInstance(TEST_LOCALE);
    /* Check for the correct currency code */
    harness.check(
        currency.getCurrencyCode(),
        ISO4217_CODE,
        "ISO 4217 currency code retrieval check (" + currency.getCurrencyCode() + ").");
    /* Check for the correct currency symbol */
    harness.check(
        currency.getSymbol(),
        CURRENCY_SYMBOL,
        "Currency symbol retrieval check (" + currency.getSymbol() + ").");
    /* Check for the correct fraction digits */
    harness.check(
        currency.getDefaultFractionDigits(),
        FRACTION_DIGITS,
        "Currency fraction digits retrieval check (" + currency.getDefaultFractionDigits() + ").");
    /* Check for the correct currency code from toString()*/
    harness.check(
        currency.toString(),
        ISO4217_CODE,
        "ISO 4217 currency code retrieval check (" + currency.toString() + ").");
  }
  /** @tests java.text.DecimalFormatSymbols#setInternationalCurrencySymbol(java.lang.String) */
  @TestTargetNew(
      level = TestLevel.COMPLETE,
      notes = "",
      method = "setInternationalCurrencySymbol",
      args = {java.lang.String.class})
  @KnownFailure("getCurrency() doesn't return null for bogus currency code.")
  public void test_setInternationalCurrencySymbolLjava_lang_String() {
    Locale locale = Locale.CANADA;
    DecimalFormatSymbols dfs =
        ((DecimalFormat) NumberFormat.getCurrencyInstance(locale)).getDecimalFormatSymbols();
    Currency currency = Currency.getInstance("JPY");
    dfs.setInternationalCurrencySymbol(currency.getCurrencyCode());

    assertTrue("Test1: Returned incorrect currency", currency == dfs.getCurrency());
    assertEquals(
        "Test1: Returned incorrect currency symbol",
        currency.getSymbol(locale),
        dfs.getCurrencySymbol());
    assertTrue(
        "Test1: Returned incorrect international currency symbol",
        currency.getCurrencyCode().equals(dfs.getInternationalCurrencySymbol()));

    String symbol = dfs.getCurrencySymbol();
    dfs.setInternationalCurrencySymbol("bogus");
    assertNull("Test2: Returned incorrect currency", dfs.getCurrency());
    assertTrue("Test2: Returned incorrect currency symbol", dfs.getCurrencySymbol().equals(symbol));
    assertEquals(
        "Test2: Returned incorrect international currency symbol",
        "bogus",
        dfs.getInternationalCurrencySymbol());
  }
  /** @tests java.text.DecimalFormatSymbols#setCurrency(java.util.Currency) */
  @TestTargetNew(
      level = TestLevel.COMPLETE,
      notes = "",
      method = "setCurrency",
      args = {java.util.Currency.class})
  public void test_setCurrencyLjava_util_Currency() {
    Locale locale = Locale.CANADA;
    DecimalFormatSymbols dfs =
        ((DecimalFormat) NumberFormat.getCurrencyInstance(locale)).getDecimalFormatSymbols();

    try {
      dfs.setCurrency(null);
      fail("Expected NullPointerException");
    } catch (NullPointerException e) {
    }

    Currency currency = Currency.getInstance("JPY");
    dfs.setCurrency(currency);

    assertTrue("Returned incorrect currency", currency == dfs.getCurrency());
    assertEquals(
        "Returned incorrect currency symbol", currency.getSymbol(locale), dfs.getCurrencySymbol());
    assertTrue(
        "Returned incorrect international currency symbol",
        currency.getCurrencyCode().equals(dfs.getInternationalCurrencySymbol()));
  }
Beispiel #4
0
    /** Set the expected and actual values from their string representations */
    private void setValuesFromStrings() {
      String blurb = "";
      String origEv = getEv();
      String origAv = getAv();
      Currency curr = Currency.getInstance(Locale.getDefault());
      String currSymbol = curr.getSymbol();
      setAv(getAv().replace(currSymbol, ""));
      setEv(getEv().replace(currSymbol, ""));

      if (!getComp().equalsIgnoreCase("between")) {
        try {
          expected = BigDecimal.valueOf(Double.valueOf(getEv()));
        } catch (Exception ex) {
          blurb = "Invalid (expected) Currency value: " + Util.sq(origEv) + "  ";
        }
      }

      try {
        actual = BigDecimal.valueOf(Double.valueOf(getAv()));
      } catch (Exception ex) {
        blurb += "Invalid (actual) Currency value: " + Util.sq(origAv) + "  ";
      }

      if (!isBlank(blurb)) {
        isSpecError = true;
        addError(blurb + "Verification aborted.");
      }
    } // setValuesFromStrings
Beispiel #5
0
 /**
  * This method tries to evaluate the localized symbol name for a {@link CurrencyUnit}. It uses
  * {@link Currency#getSymbol(Locale)} if the given currency code maps to a JDK {@link Currency}
  * instance.
  *
  * <p>If not found {@code currency.getCurrencyCode()} is returned.
  *
  * @param currency The currency, not {@code null}
  * @return the formatted currency symbol.
  */
 private String getCurrencySymbol(CurrencyUnit currency) {
   Currency jdkCurrency = getCurrency(currency.getCurrencyCode());
   if (jdkCurrency != null) {
     return jdkCurrency.getSymbol(locale);
   }
   return currency.getCurrencyCode();
 }
Beispiel #6
0
 private static String a(Locale locale, double d1, Currency currency, boolean flag)
 {
     String s;
     String s1;
     StringBuilder stringbuilder;
     boolean flag1;
     if (a(locale, currency) == 0)
     {
         flag1 = true;
     } else
     {
         flag1 = false;
     }
     s = currency.getSymbol();
     s1 = currency.getCurrencyCode();
     try
     {
         stringbuilder = new StringBuilder();
     }
     // Misplaced declaration of an exception variable
     catch (Locale locale)
     {
         return "";
     }
     if (flag1) goto _L2; else goto _L1
 /**
  * Sets the currency of these DecimalFormatSymbols. This also sets the currency symbol attribute
  * to the currency's symbol in the DecimalFormatSymbols' locale, and the international currency
  * symbol attribute to the currency's ISO 4217 currency code.
  *
  * @param currency the new currency to be used
  * @exception NullPointerException if <code>currency</code> is null
  * @since 1.4
  * @see #setCurrencySymbol
  * @see #setInternationalCurrencySymbol
  */
 public void setCurrency(Currency currency) {
   if (currency == null) {
     throw new NullPointerException();
   }
   this.currency = currency;
   intlCurrencySymbol = currency.getCurrencyCode();
   currencySymbol = currency.getSymbol(locale);
 }
Beispiel #8
0
 public String toString(Locale locale) {
   String symbol = currency.getSymbol(locale);
   if (!"$".equals(symbol)) {
     symbol = symbol + " ";
   }
   return symbol
       + NumberFormatter.commas(amount.doubleValue(), currency.getDefaultFractionDigits());
 }
  @Override
  public boolean equals(Object object) {
    if (object == this) {
      return true;
    }
    if (!(object instanceof DecimalFormat)) {
      return false;
    }
    DecimalFormat obj = (DecimalFormat) object;

    if (obj.addr == this.addr) {
      return true;
    }

    boolean result = super.equals(object);

    result &= obj.toPattern().equals(this.toPattern());
    result &= obj.isDecimalSeparatorAlwaysShown() == this.isDecimalSeparatorAlwaysShown();
    result &= obj.getGroupingSize() == this.getGroupingSize();
    result &= obj.getMultiplier() == this.getMultiplier();
    result &= obj.getNegativePrefix().equals(this.getNegativePrefix());
    result &= obj.getNegativeSuffix().equals(this.getNegativeSuffix());
    result &= obj.getPositivePrefix().equals(this.getPositivePrefix());
    result &= obj.getPositiveSuffix().equals(this.getPositiveSuffix());
    result &= obj.getMaximumIntegerDigits() == this.getMaximumIntegerDigits();
    result &= obj.getMaximumFractionDigits() == this.getMaximumFractionDigits();
    result &= obj.getMinimumIntegerDigits() == this.getMinimumIntegerDigits();
    result &= obj.getMinimumFractionDigits() == this.getMinimumFractionDigits();
    result &= obj.isGroupingUsed() == this.isGroupingUsed();
    Currency objCurr = obj.getCurrency();
    Currency thisCurr = this.getCurrency();
    if (objCurr != null) {
      result &= objCurr.getCurrencyCode().equals(thisCurr.getCurrencyCode());
      result &= objCurr.getSymbol().equals(thisCurr.getSymbol());
      result &= objCurr.getDefaultFractionDigits() == thisCurr.getDefaultFractionDigits();
    } else {
      result &= thisCurr == null;
    }
    result &= obj.getDecimalFormatSymbols().equals(this.getDecimalFormatSymbols());

    return result;
  }
 public static String getCountryCodeFreeCurrencySymbol(Currency currency) {
   String currencySymbol = currency.getSymbol();
   if (currencySymbol == null) {
     currencySymbol = "";
   } else if (currencySymbol.indexOf('$') != -1) {
     // For $ based currencies (USD, CAD, HKD, etc.) we only display the currency symbol without
     // any form of the international code. This is a hack! This revolves
     // around the fact that not all devices have the ability to specify all locales. For example,
     // Android docs state that some phones may have en_US while others
     // may not. This is a terrible design flaw on the Android platform side; these formats are
     // standards, not opinions. A very 'close to home' example of this is
     // that 'Canada' is not available on Nexus devices. You can specify that you speak english,
     // but not that you're in Canada. Therefore, en_CA is unavailable
     // and en_US formats CAD as 'CA$5.00'
     currencySymbol = "$";
   }
   return currencySymbol;
 }
  public void printAvailableLocales(OutputStream os) throws Exception {
    Locale[] locales = Locale.getAvailableLocales();

    String name = null, country = null, lang = null, currencySb = null;
    String countryCd = null, langCd = null, currencyCd = null;
    Currency currency = null;

    PrintWriter pw = null;

    try {
      pw = new PrintWriter(os, true);

      for (Locale locale : locales) {
        name = locale.getDisplayName();
        country = locale.getDisplayCountry();
        countryCd = locale.getISOCountries()[0];
        lang = locale.getDisplayLanguage();
        langCd = locale.getISOLanguages()[0];

        try {
          currency = Currency.getInstance(locale);
          currencySb = currency.getSymbol();
          currencyCd = currency.getCurrencyCode();
        } catch (IllegalArgumentException ex) {
          currencySb = "";
          currencyCd = "";
        }

        pw.print("name : " + name + ", ");
        pw.print("country : " + country + "(" + countryCd + "), ");
        pw.print("lang : " + lang + "(" + langCd + "), ");
        pw.println("currency : " + currencySb + "(" + currencyCd + ")");
      }
    } catch (Exception ex) {
      throw ex;
    } finally {
      if (pw != null && os != (OutputStream) (System.out)) pw.close();
    }
  }
Beispiel #12
0
 /** Side-step Currency.getSymbol() only returning a symbol if given a matching locale. */
 private static String getRealSymbol(Currency c) {
   switch (c.getCurrencyCode()) {
     case "USD":
       return "$";
     case "AUD":
       return "AU$";
     case "CAD":
       return "CA$";
     case "NZD":
       return "NZ$";
     case "BRL":
       return "R$";
     case "EUR":
       return "€";
     case "GBP":
       return "£";
     case "SEK":
     case "DKK":
     case "NOK":
       return "kr";
     case "SGD":
       return "S$";
     case "HKD":
       return "HK$";
     case "INR":
       return "₹";
     case "JPY":
       return "¥";
     case "ZAR":
       return "R";
     case "CHF":
       return "CHF";
     default:
       return c.getSymbol();
   }
 }
 // get the currency symbol for a currency viewed through a particular locale.
 // The symbol for USD us $ when viewed from the en_US locale.
 // The symbol for USD us US$ when viewed from the en_IE locale.
 public static String getSymbolForCurrency(String pCurrencyCode, java.util.Locale pLoc) {
   java.util.Currency ocurrCd = java.util.Currency.getInstance(pCurrencyCode);
   return ocurrCd.getSymbol(pLoc);
 }
Beispiel #14
0
 public String toString() {
   return fAmount.toPlainString() + " " + fCurrency.getSymbol();
 }
Beispiel #15
0
  static String describeLocale(String name) {
    final StringBuilder result = new StringBuilder();
    result.append("<html>");

    final Locale locale = localeByName(name);

    result.append("<p>");
    append(result, "Display Name", locale.getDisplayName());
    append(result, "Localized Display Name", locale.getDisplayName(locale));

    if (locale.getLanguage().length() > 0) {
      String iso3Language = "(not available)";
      try {
        iso3Language = locale.getISO3Language();
      } catch (MissingResourceException ignored) {
      }

      result.append("<p>");
      append(result, "Display Language", locale.getDisplayLanguage());
      append(result, "Localized Display Language", locale.getDisplayLanguage(locale));
      append(result, "2-Letter Language Code", locale.getLanguage());
      append(result, "3-Letter Language Code", iso3Language);
    }
    if (locale.getCountry().length() > 0) {
      String iso3Country = "(not available)";
      try {
        iso3Country = locale.getISO3Country();
      } catch (MissingResourceException ignored) {
      }

      result.append("<p>");
      append(result, "Display Country", locale.getDisplayCountry());
      append(result, "Localized Display Country", locale.getDisplayCountry(locale));
      append(result, "2-Letter Country Code", locale.getCountry());
      append(result, "3-Letter Country Code", iso3Country);
    }
    if (locale.getVariant().length() > 0) {
      result.append("<p>");
      append(result, "Display Variant", locale.getDisplayVariant());
      append(result, "Localized Display Variant", locale.getDisplayVariant(locale));
      append(result, "Variant Code", locale.getVariant());
    }

    result.append("<p><b>Number Formatting</b>");
    describeNumberFormat(result, "Decimal", NumberFormat.getInstance(locale), 1234.5, -1234.5);
    describeNumberFormat(result, "Integer", NumberFormat.getIntegerInstance(locale), 1234, -1234);
    describeNumberFormat(
        result, "Currency", NumberFormat.getCurrencyInstance(locale), 1234.5, -1234.5);
    describeNumberFormat(result, "Percent", NumberFormat.getPercentInstance(locale), 12.3);

    boolean hasLocaleData = hasLocaleData();

    if (!hasLocaleData) {
      result.append("<p><b>Decimal Format Symbols</b>");
      NumberFormat nf = NumberFormat.getInstance(locale);
      if (nf instanceof DecimalFormat) {
        describeDecimalFormatSymbols(result, ((DecimalFormat) nf).getDecimalFormatSymbols());
      } else {
        result.append("(Didn't expect " + nf.getClass() + ".)");
      }
    }

    Date now =
        new Date(); // FIXME: it might be more useful to always show a time in the afternoon, to
    // make 24-hour patterns more obvious.
    result.append("<p><b>Date/Time Formatting</b>");
    describeDateFormat(
        result, "Full Date", DateFormat.getDateInstance(DateFormat.FULL, locale), now);
    describeDateFormat(
        result, "Long Date", DateFormat.getDateInstance(DateFormat.LONG, locale), now);
    describeDateFormat(
        result, "Medium Date", DateFormat.getDateInstance(DateFormat.MEDIUM, locale), now);
    describeDateFormat(
        result, "Short Date", DateFormat.getDateInstance(DateFormat.SHORT, locale), now);
    result.append("<p>");
    describeDateFormat(
        result, "Full Time", DateFormat.getTimeInstance(DateFormat.FULL, locale), now);
    describeDateFormat(
        result, "Long Time", DateFormat.getTimeInstance(DateFormat.LONG, locale), now);
    describeDateFormat(
        result, "Medium Time", DateFormat.getTimeInstance(DateFormat.MEDIUM, locale), now);
    describeDateFormat(
        result, "Short Time", DateFormat.getTimeInstance(DateFormat.SHORT, locale), now);
    result.append("<p>");
    describeDateFormat(
        result,
        "Full Date/Time",
        DateFormat.getDateTimeInstance(DateFormat.FULL, DateFormat.FULL, locale),
        now);
    describeDateFormat(
        result,
        "Long Date/Time",
        DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.LONG, locale),
        now);
    describeDateFormat(
        result,
        "Medium Date/Time",
        DateFormat.getDateTimeInstance(DateFormat.MEDIUM, DateFormat.MEDIUM, locale),
        now);
    describeDateFormat(
        result,
        "Short Date/Time",
        DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.SHORT, locale),
        now);

    if (!hasLocaleData) {
      result.append("<p><b>Date Format Symbols</b><p>");
      DateFormat edf = DateFormat.getDateInstance(DateFormat.FULL, Locale.US);
      DateFormatSymbols edfs = ((SimpleDateFormat) edf).getDateFormatSymbols();
      DateFormat df = DateFormat.getDateInstance(DateFormat.FULL, locale);
      DateFormatSymbols dfs = ((SimpleDateFormat) df).getDateFormatSymbols();
      append(result, "Local Pattern Chars", dfs.getLocalPatternChars());
      append(result, "Am/pm", Arrays.toString(dfs.getAmPmStrings()));
      append(result, "Eras", Arrays.toString(dfs.getEras()));
      append(result, "Months", Arrays.toString(dfs.getMonths()));
      append(result, "Short Months", Arrays.toString(dfs.getShortMonths()));
      append(result, "Weekdays", Arrays.toString(dfs.getWeekdays()));
      append(result, "Short Weekdays", Arrays.toString(dfs.getShortWeekdays()));
    }

    result.append("<p><b>Calendar</b><p>");
    Calendar c = Calendar.getInstance(locale);
    int firstDayOfWeek = c.getFirstDayOfWeek();
    String firstDayOfWeekString = new DateFormatSymbols(locale).getWeekdays()[firstDayOfWeek];
    String englishFirstDayOfWeekString =
        new DateFormatSymbols(Locale.US).getWeekdays()[firstDayOfWeek];
    String firstDayOfWeekDetails = firstDayOfWeek + " '" + firstDayOfWeekString + "'";
    if (!englishFirstDayOfWeekString.equals(firstDayOfWeekString)) {
      firstDayOfWeekDetails += " (" + englishFirstDayOfWeekString + ")";
    }
    append(result, "First Day of the Week", firstDayOfWeekDetails);
    append(result, "Minimal Days in First Week", c.getMinimalDaysInFirstWeek());

    // If this locale specifies a country, check out the currency.
    // Languages don't have currencies; countries do.
    if (!locale.getCountry().equals("")) {
      result.append("<p><b>Currency</b><p>");
      try {
        Currency currency = Currency.getInstance(locale);
        append(result, "ISO 4217 Currency Code", currency.getCurrencyCode());
        append(
            result,
            "Currency Symbol",
            unicodeString(currency.getSymbol(locale)) + " (" + currency.getSymbol(Locale.US) + ")");
        append(result, "Default Fraction Digits", currency.getDefaultFractionDigits());
      } catch (IllegalArgumentException ex) {
        result.append(
            "<p>(This version of Android is unable to return a Currency for this Locale.)");
      }
    }

    result.append("<p><b>Data Availability</b><p>");
    appendAvailability(result, locale, "BreakIterator", BreakIterator.class);
    appendAvailability(result, locale, "Calendar", NumberFormat.class);
    appendAvailability(result, locale, "Collator", Collator.class);
    appendAvailability(result, locale, "DateFormat", DateFormat.class);
    appendAvailability(result, locale, "DateFormatSymbols", DateFormatSymbols.class);
    appendAvailability(result, locale, "DecimalFormatSymbols", DecimalFormatSymbols.class);
    appendAvailability(result, locale, "NumberFormat", NumberFormat.class);

    if (hasLocaleData) {
      result.append("<p><b>libcore.icu.LocaleData</b>");
      try {
        Object enUsData = getLocaleDataInstance(Locale.US);
        Object localeData = getLocaleDataInstance(locale);
        String[] previous;

        result.append("<p>");
        describeStringArray(result, "amPm", enUsData, localeData, null);
        describeStringArray(result, "eras", enUsData, localeData, null);

        result.append("<p>");
        previous = describeStringArray(result, "longMonthNames", enUsData, localeData, null);
        describeStringArray(result, "longStandAloneMonthNames", enUsData, localeData, previous);
        previous = describeStringArray(result, "shortMonthNames", enUsData, localeData, null);
        describeStringArray(result, "shortStandAloneMonthNames", enUsData, localeData, previous);
        previous = describeStringArray(result, "tinyMonthNames", enUsData, localeData, null);
        describeStringArray(result, "tinyStandAloneMonthNames", enUsData, localeData, previous);

        result.append("<p>");
        previous = describeStringArray(result, "longWeekdayNames", enUsData, localeData, null);
        describeStringArray(result, "longStandAloneWeekdayNames", enUsData, localeData, previous);
        previous = describeStringArray(result, "shortWeekdayNames", enUsData, localeData, null);
        describeStringArray(result, "shortStandAloneWeekdayNames", enUsData, localeData, previous);
        previous = describeStringArray(result, "tinyWeekdayNames", enUsData, localeData, null);
        describeStringArray(result, "tinyStandAloneWeekdayNames", enUsData, localeData, previous);

        result.append("<p>");
        describeString(result, "yesterday", enUsData, localeData);
        describeString(result, "today", enUsData, localeData);
        describeString(result, "tomorrow", enUsData, localeData);

        result.append("<p>");
        describeString(result, "timeFormat12", enUsData, localeData);
        describeString(result, "timeFormat24", enUsData, localeData);

        result.append("<p>");
        describeChar(result, "zeroDigit", enUsData, localeData);
        describeChar(result, "decimalSeparator", enUsData, localeData);
        describeChar(result, "groupingSeparator", enUsData, localeData);
        describeChar(result, "patternSeparator", enUsData, localeData);
        describeChar(result, "percent", enUsData, localeData);
        describeChar(result, "perMill", enUsData, localeData);
        describeChar(result, "monetarySeparator", enUsData, localeData);
        describeChar(result, "minusSign", enUsData, localeData);
        describeString(result, "exponentSeparator", enUsData, localeData);
        describeString(result, "infinity", enUsData, localeData);
        describeString(result, "NaN", enUsData, localeData);

      } catch (Exception ex) {
        result.append("(" + ex.getClass().getSimpleName() + " thrown: " + ex.getMessage() + ")");
        System.err.println(ex);
      }
    }

    return result.toString();
  }