Java Code Examples for java.text.DecimalFormatSymbols#setCurrencySymbol()
The following examples show how to use
java.text.DecimalFormatSymbols#setCurrencySymbol() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: NumberFormatTest.java From j2objc with Apache License 2.0 | 7 votes |
public void test_issue79925() { NumberFormat nf = NumberFormat.getCurrencyInstance(Locale.US); nf.setCurrency(Currency.getInstance("EUR")); assertEquals("€50.00", nf.format(50.0)); DecimalFormatSymbols decimalFormatSymbols = ((DecimalFormat) nf).getDecimalFormatSymbols(); decimalFormatSymbols.setCurrencySymbol(""); ((DecimalFormat) nf).setDecimalFormatSymbols(decimalFormatSymbols); assertEquals("50.00", nf.format(50.0)); nf.setCurrency(Currency.getInstance("SGD")); assertEquals("SGD50.00", nf.format(50.0)); nf.setCurrency(Currency.getInstance("SGD")); assertEquals("SGD50.00", nf.format(50.00)); nf.setCurrency(Currency.getInstance("USD")); assertEquals("$50.00", nf.format(50.0)); nf.setCurrency(Currency.getInstance("SGD")); assertEquals("SGD50.00", nf.format(50.0)); }
Example 2
Source File: NumberFormatter.java From birt with Eclipse Public License 1.0 | 6 votes |
private DecimalFormatSymbols getICUDecimalSymbols( Locale locale ) { DecimalFormatSymbols symbols = new DecimalFormatSymbols( locale ); com.ibm.icu.text.DecimalFormatSymbols icuSymbols = new com.ibm.icu.text.DecimalFormatSymbols( locale ); symbols.setCurrencySymbol( icuSymbols.getCurrencySymbol( ) ); symbols.setDecimalSeparator( icuSymbols.getDecimalSeparator( ) ); symbols.setDigit( icuSymbols.getDigit( ) ); symbols.setGroupingSeparator( icuSymbols.getGroupingSeparator( ) ); symbols.setInfinity( icuSymbols.getInfinity( ) ); symbols.setInternationalCurrencySymbol( icuSymbols .getInternationalCurrencySymbol( ) ); symbols.setMinusSign( icuSymbols.getMinusSign( ) ); symbols.setMonetaryDecimalSeparator( icuSymbols .getMonetaryDecimalSeparator( ) ); symbols.setNaN( icuSymbols.getNaN( ) ); symbols.setPatternSeparator( icuSymbols.getPatternSeparator( ) ); symbols.setPercent( icuSymbols.getPercent( ) ); symbols.setPerMill( icuSymbols.getPerMill( ) ); symbols.setZeroDigit( icuSymbols.getZeroDigit( ) ); return symbols; }
Example 3
Source File: NumberFormatTest.java From j2objc with Apache License 2.0 | 6 votes |
public void test_customCurrencySymbol() { NumberFormat nf = NumberFormat.getCurrencyInstance(Locale.US); DecimalFormatSymbols dfs = ((DecimalFormat) nf).getDecimalFormatSymbols(); dfs.setCurrencySymbol("SPECIAL"); ((DecimalFormat) nf).setDecimalFormatSymbols(dfs); assertEquals("SPECIAL3.14", nf.format(3.14)); // Setting the currency again should reset the symbols. nf.setCurrency(Currency.getInstance("USD")); assertEquals("$3.14", nf.format(3.14)); // Setting it back again should work. dfs.setCurrencySymbol("NEW"); ((DecimalFormat) nf).setDecimalFormatSymbols(dfs); assertEquals("NEW3.14", nf.format(3.14)); }
Example 4
Source File: DFSSerialization.java From openjdk-jdk9 with GNU General Public License v2.0 | 6 votes |
private File createTestObject(String objectName, String expString){ DecimalFormatSymbols dfs= new DecimalFormatSymbols(); dfs.setExponentSeparator(expString); dfs.setCurrencySymbol("*SpecialCurrencySymbol*"); logln(" The special exponent separator is set : " + dfs.getExponentSeparator()); logln(" The special currency symbol is set : " + dfs.getCurrencySymbol()); // 6345659: create a test object in the test.class dir where test user has a write permission. File file = new File(System.getProperty("test.class", "."), objectName); try (FileOutputStream ostream = new FileOutputStream(file)) { ObjectOutputStream p = new ObjectOutputStream(ostream); p.writeObject(dfs); //System.out.println(" The special currency symbol is set : " + dfs.getCurrencySymbol()); return file; } catch (Exception e){ errln("Test Malfunction in DFSSerialization: Exception while creating an object"); /* * logically should not throw this exception as errln throws exception * if not thrown yet - but in case errln got changed */ throw new RuntimeException("Test Malfunction: re-throwing the exception", e); } }
Example 5
Source File: Helper.java From smartcoins-wallet with MIT License | 6 votes |
public static boolean isRTL(Locale locale, String symbol) { NumberFormat currencyFormat = NumberFormat.getCurrencyInstance(locale); // We then tell our formatter to use this symbol. DecimalFormatSymbols decimalFormatSymbols = ((java.text.DecimalFormat) currencyFormat).getDecimalFormatSymbols(); decimalFormatSymbols.setCurrencySymbol(symbol); ((java.text.DecimalFormat) currencyFormat).setDecimalFormatSymbols(decimalFormatSymbols); String formattedtext = currencyFormat.format(100.0); if (formattedtext.startsWith(symbol)) { return false; } else { return true; } }
Example 6
Source File: Helper.java From smartcoins-wallet with MIT License | 6 votes |
public static NumberFormat newCurrencyFormat(Context context, Currency currency, Locale displayLocale) { Log.d(TAG, "newCurrencyFormat"); NumberFormat retVal = NumberFormat.getCurrencyInstance(displayLocale); retVal.setCurrency(currency); //The default JDK handles situations well when the currency is the default currency for the locale // if (currency.equals(Currency.getInstance(displayLocale))) { // Log.d(TAG, "Let the JDK handle this"); // return retVal; // } //otherwise we need to "fix things up" when displaying a non-native currency if (retVal instanceof DecimalFormat) { DecimalFormat decimalFormat = (DecimalFormat) retVal; String correctedI18NSymbol = getCorrectedInternationalCurrencySymbol(context, currency, displayLocale); if (correctedI18NSymbol != null) { DecimalFormatSymbols dfs = decimalFormat.getDecimalFormatSymbols(); //this returns a clone of DFS dfs.setInternationalCurrencySymbol(correctedI18NSymbol); dfs.setCurrencySymbol(correctedI18NSymbol); decimalFormat.setDecimalFormatSymbols(dfs); } } return retVal; }
Example 7
Source File: NormalValuesFormatter.java From fingen with Apache License 2.0 | 5 votes |
public NormalValuesFormatter() { mFormat = NumberFormat.getCurrencyInstance(); mFormat.setMinimumFractionDigits(0); mFormat.setMaximumFractionDigits(2); DecimalFormatSymbols dfs = new DecimalFormatSymbols(); dfs.setCurrencySymbol(""); dfs.setGroupingSeparator(' '); dfs.setMonetaryDecimalSeparator('.'); ((DecimalFormat) mFormat).setDecimalFormatSymbols(dfs); }
Example 8
Source File: CabbageFormatter.java From fingen with Apache License 2.0 | 5 votes |
public synchronized String format(BigDecimal amount){ if (mCabbage != null) { mNumberFormat.setMinimumFractionDigits(mCabbage.getDecimalCount()); mNumberFormat.setMaximumFractionDigits(mCabbage.getDecimalCount()); DecimalFormatSymbols dfs = new DecimalFormatSymbols(); dfs.setCurrencySymbol(mCabbage.getSimbol()); dfs.setGroupingSeparator(' '); dfs.setMonetaryDecimalSeparator('.'); ((DecimalFormat) mNumberFormat).setDecimalFormatSymbols(dfs); return mNumberFormat.format(amount.setScale(mCabbage.getDecimalCount(), mRoundingMode)); } else { return ""; } }
Example 9
Source File: Bill.java From rally with Apache License 2.0 | 5 votes |
public Bill(String name, float amount, Date dueDate, int color) { mName = name; mAmount = amount; mDueDate = dueDate; mColor = color; mFormatter = (DecimalFormat) NumberFormat.getCurrencyInstance(); DecimalFormatSymbols symbols = mFormatter.getDecimalFormatSymbols(); symbols.setCurrencySymbol(""); mFormatter.setDecimalFormatSymbols(symbols); }
Example 10
Source File: Account.java From rally with Apache License 2.0 | 5 votes |
public Account(String name, float amount, String lastFourDigits, int color) { mName = name; mAmount = amount; mLastFourDigits = lastFourDigits; mColor = color; mFormatter = (DecimalFormat) NumberFormat.getCurrencyInstance(); DecimalFormatSymbols symbols = mFormatter.getDecimalFormatSymbols(); symbols.setCurrencySymbol(""); mFormatter.setDecimalFormatSymbols(symbols); }
Example 11
Source File: BtcFormat.java From green_android with GNU General Public License v3.0 | 5 votes |
/** Set the currency symbol and international code of the underlying {@link * java.text.NumberFormat} object to the values of the last two arguments, respectively. * This method is invoked in the process of parsing, not formatting. * * Only invoke this from code synchronized on value of the first argument, and don't * forget to put the symbols back otherwise equals(), hashCode() and immutability will * break. */ private static DecimalFormatSymbols setSymbolAndCode(DecimalFormat numberFormat, String symbol, String code) { checkState(Thread.holdsLock(numberFormat)); DecimalFormatSymbols fs = numberFormat.getDecimalFormatSymbols(); DecimalFormatSymbols ante = (DecimalFormatSymbols)fs.clone(); fs.setInternationalCurrencySymbol(code); fs.setCurrencySymbol(symbol); numberFormat.setDecimalFormatSymbols(fs); return ante; }
Example 12
Source File: Value.java From pentaho-kettle with Apache License 2.0 | 5 votes |
public Value num2str( String format, String decimalSymbol, String groupingSymbol, String currencySymbol ) throws KettleValueException { if ( isNull() ) { setType( VALUE_TYPE_STRING ); } else { // Number to String conversion... if ( getType() == VALUE_TYPE_NUMBER || getType() == VALUE_TYPE_INTEGER ) { NumberFormat nf = NumberFormat.getInstance(); DecimalFormat df = (DecimalFormat) nf; DecimalFormatSymbols dfs = new DecimalFormatSymbols(); if ( currencySymbol != null && currencySymbol.length() > 0 ) { dfs.setCurrencySymbol( currencySymbol ); } if ( groupingSymbol != null && groupingSymbol.length() > 0 ) { dfs.setGroupingSeparator( groupingSymbol.charAt( 0 ) ); } if ( decimalSymbol != null && decimalSymbol.length() > 0 ) { dfs.setDecimalSeparator( decimalSymbol.charAt( 0 ) ); } df.setDecimalFormatSymbols( dfs ); // in case of 4, 3 or 2 if ( format != null && format.length() > 0 ) { df.applyPattern( format ); } try { setValue( nf.format( getNumber() ) ); } catch ( Exception e ) { setType( VALUE_TYPE_STRING ); setNull(); throw new KettleValueException( "Couldn't convert Number to String " + e.toString() ); } } else { throw new KettleValueException( "Function NUM2STR only works on Numbers and Integers" ); } } return this; }
Example 13
Source File: CurrencyEditText.java From FormattEditText with Apache License 2.0 | 5 votes |
private void updateText() { DecimalFormat formatter = getCurrencyFormatter(); current = formatter.format(value); // Now we need to find clear string without currency symbols DecimalFormatSymbols symbols = formatter.getDecimalFormatSymbols(); symbols.setCurrencySymbol(""); formatter.setDecimalFormatSymbols(symbols); String formattedClear = formatter.format(value); // Currency symbols may be placed with spaces (e.g. nbsp) at start or at end of string int start = 0; if (Character.isSpaceChar(formattedClear.charAt(start))) ++start; int end = formattedClear.length() - 1; if (Character.isSpaceChar(formattedClear.charAt(end))) --end; // Trim spaces (String.trim() may skip most spaces) formattedClear = formattedClear.substring(start, end + 1); // Set position of cursor at end of clear string in formatted string int pos = current.indexOf(formattedClear) + formattedClear.length(); removeTextChangedListener(textWatcher); setText(current); setSelection(Math.min(pos, current.length())); addTextChangedListener(textWatcher); }
Example 14
Source File: BtcFormat.java From GreenBits with GNU General Public License v3.0 | 5 votes |
/** Set the currency symbol and international code of the underlying {@link * java.text.NumberFormat} object to the values of the last two arguments, respectively. * This method is invoked in the process of parsing, not formatting. * * Only invoke this from code synchronized on value of the first argument, and don't * forget to put the symbols back otherwise equals(), hashCode() and immutability will * break. */ private static DecimalFormatSymbols setSymbolAndCode(DecimalFormat numberFormat, String symbol, String code) { checkState(Thread.holdsLock(numberFormat)); DecimalFormatSymbols fs = numberFormat.getDecimalFormatSymbols(); DecimalFormatSymbols ante = (DecimalFormatSymbols)fs.clone(); fs.setInternationalCurrencySymbol(code); fs.setCurrencySymbol(symbol); numberFormat.setDecimalFormatSymbols(fs); return ante; }
Example 15
Source File: SakaiDecimalFormatSymbolsProvider.java From sakai with Educational Community License v2.0 | 4 votes |
/** * {@inheritDoc} */ @Override public DecimalFormatSymbols getInstance(final Locale locale) throws IllegalArgumentException, NullPointerException { if (locale == null) { throw new NullPointerException("locale:null"); } else if (!SakaiLocaleServiceProviderUtil.isAvailableLocale(locale)) { throw new IllegalArgumentException("locale:" + locale.toString()); } DecimalFormatSymbols symbols = new DecimalFormatSymbols(); symbols.setDecimalSeparator( SakaiLocaleServiceProviderUtil.getChar("DecimalSeparator", locale)); symbols.setDigit( SakaiLocaleServiceProviderUtil.getChar("Digit", locale)); symbols.setExponentSeparator( SakaiLocaleServiceProviderUtil.getString("ExponentSeparator", locale)); symbols.setGroupingSeparator( SakaiLocaleServiceProviderUtil.getChar("GroupingSeparator", locale)); symbols.setInfinity( SakaiLocaleServiceProviderUtil.getString("Infinity", locale)); symbols.setInternationalCurrencySymbol( SakaiLocaleServiceProviderUtil.getString("InternationalCurrencySymbol", locale)); symbols.setCurrencySymbol( SakaiLocaleServiceProviderUtil.getString("CurrencySymbol", locale)); symbols.setMinusSign( SakaiLocaleServiceProviderUtil.getChar("MinusSign", locale)); symbols.setMonetaryDecimalSeparator( SakaiLocaleServiceProviderUtil.getChar("MonetaryDecimalSeparator", locale)); symbols.setNaN( SakaiLocaleServiceProviderUtil.getString("NaN", locale)); symbols.setPatternSeparator( SakaiLocaleServiceProviderUtil.getChar("PatternSeparator", locale)); symbols.setPercent( SakaiLocaleServiceProviderUtil.getChar("Percent", locale)); symbols.setPerMill( SakaiLocaleServiceProviderUtil.getChar("PerMill", locale)); symbols.setZeroDigit( SakaiLocaleServiceProviderUtil.getChar("ZeroDigit", locale)); return symbols; }
Example 16
Source File: Value.java From pentaho-kettle with Apache License 2.0 | 4 votes |
public Value str2num( String pattern, String decimal, String grouping, String currency ) throws KettleValueException { // 0 : pattern // 1 : Decimal separator // 2 : Grouping separator // 3 : Currency symbol if ( isNull() ) { setType( VALUE_TYPE_STRING ); } else { if ( getType() == VALUE_TYPE_STRING ) { if ( getString() == null ) { setNull(); setValue( 0.0 ); } else { NumberFormat nf = NumberFormat.getInstance(); DecimalFormat df = (DecimalFormat) nf; DecimalFormatSymbols dfs = new DecimalFormatSymbols(); if ( !Utils.isEmpty( pattern ) ) { df.applyPattern( pattern ); } if ( !Utils.isEmpty( decimal ) ) { dfs.setDecimalSeparator( decimal.charAt( 0 ) ); } if ( !Utils.isEmpty( grouping ) ) { dfs.setGroupingSeparator( grouping.charAt( 0 ) ); } if ( !Utils.isEmpty( currency ) ) { dfs.setCurrencySymbol( currency ); } try { df.setDecimalFormatSymbols( dfs ); setValue( df.parse( getString() ).doubleValue() ); } catch ( Exception e ) { String message = "Couldn't convert string to number " + e.toString(); if ( !Utils.isEmpty( pattern ) ) { message += " pattern=" + pattern; } if ( !Utils.isEmpty( decimal ) ) { message += " decimal=" + decimal; } if ( !Utils.isEmpty( grouping ) ) { message += " grouping=" + grouping.charAt( 0 ); } if ( !Utils.isEmpty( currency ) ) { message += " currency=" + currency; } throw new KettleValueException( message ); } } } else { throw new KettleValueException( "Function STR2NUM works only on strings" ); } } return this; }
Example 17
Source File: SakaiDecimalFormatSymbolsProvider.java From sakai with Educational Community License v2.0 | 4 votes |
/** * {@inheritDoc} */ @Override public DecimalFormatSymbols getInstance(final Locale locale) throws IllegalArgumentException, NullPointerException { if (locale == null) { throw new NullPointerException("locale:null"); } else if (!SakaiLocaleServiceProviderUtil.isAvailableLocale(locale)) { throw new IllegalArgumentException("locale:" + locale.toString()); } DecimalFormatSymbols symbols = new DecimalFormatSymbols(); symbols.setDecimalSeparator( SakaiLocaleServiceProviderUtil.getChar("DecimalSeparator", locale)); symbols.setDigit( SakaiLocaleServiceProviderUtil.getChar("Digit", locale)); symbols.setExponentSeparator( SakaiLocaleServiceProviderUtil.getString("ExponentSeparator", locale)); symbols.setGroupingSeparator( SakaiLocaleServiceProviderUtil.getChar("GroupingSeparator", locale)); symbols.setInfinity( SakaiLocaleServiceProviderUtil.getString("Infinity", locale)); symbols.setInternationalCurrencySymbol( SakaiLocaleServiceProviderUtil.getString("InternationalCurrencySymbol", locale)); symbols.setCurrencySymbol( SakaiLocaleServiceProviderUtil.getString("CurrencySymbol", locale)); symbols.setMinusSign( SakaiLocaleServiceProviderUtil.getChar("MinusSign", locale)); symbols.setMonetaryDecimalSeparator( SakaiLocaleServiceProviderUtil.getChar("MonetaryDecimalSeparator", locale)); symbols.setNaN( SakaiLocaleServiceProviderUtil.getString("NaN", locale)); symbols.setPatternSeparator( SakaiLocaleServiceProviderUtil.getChar("PatternSeparator", locale)); symbols.setPercent( SakaiLocaleServiceProviderUtil.getChar("Percent", locale)); symbols.setPerMill( SakaiLocaleServiceProviderUtil.getChar("PerMill", locale)); symbols.setZeroDigit( SakaiLocaleServiceProviderUtil.getChar("ZeroDigit", locale)); return symbols; }
Example 18
Source File: StringUtil.java From pentaho-kettle with Apache License 2.0 | 4 votes |
public static double str2num( String pattern, String decimal, String grouping, String currency, String value ) throws KettleValueException { // 0 : pattern // 1 : Decimal separator // 2 : Grouping separator // 3 : Currency symbol NumberFormat nf = NumberFormat.getInstance(); DecimalFormat df = (DecimalFormat) nf; DecimalFormatSymbols dfs = new DecimalFormatSymbols(); if ( !Utils.isEmpty( pattern ) ) { df.applyPattern( pattern ); } if ( !Utils.isEmpty( decimal ) ) { dfs.setDecimalSeparator( decimal.charAt( 0 ) ); } if ( !Utils.isEmpty( grouping ) ) { dfs.setGroupingSeparator( grouping.charAt( 0 ) ); } if ( !Utils.isEmpty( currency ) ) { dfs.setCurrencySymbol( currency ); } try { df.setDecimalFormatSymbols( dfs ); return df.parse( value ).doubleValue(); } catch ( Exception e ) { String message = "Couldn't convert string to number " + e.toString(); if ( !isEmpty( pattern ) ) { message += " pattern=" + pattern; } if ( !isEmpty( decimal ) ) { message += " decimal=" + decimal; } if ( !isEmpty( grouping ) ) { message += " grouping=" + grouping.charAt( 0 ); } if ( !isEmpty( currency ) ) { message += " currency=" + currency; } throw new KettleValueException( message ); } }
Example 19
Source File: StringUtil.java From hop with Apache License 2.0 | 4 votes |
public static double str2num( String pattern, String decimal, String grouping, String currency, String value ) throws HopValueException { // 0 : pattern // 1 : Decimal separator // 2 : Grouping separator // 3 : Currency symbol NumberFormat nf = NumberFormat.getInstance(); DecimalFormat df = (DecimalFormat) nf; DecimalFormatSymbols dfs = new DecimalFormatSymbols(); if ( !Utils.isEmpty( pattern ) ) { df.applyPattern( pattern ); } if ( !Utils.isEmpty( decimal ) ) { dfs.setDecimalSeparator( decimal.charAt( 0 ) ); } if ( !Utils.isEmpty( grouping ) ) { dfs.setGroupingSeparator( grouping.charAt( 0 ) ); } if ( !Utils.isEmpty( currency ) ) { dfs.setCurrencySymbol( currency ); } try { df.setDecimalFormatSymbols( dfs ); return df.parse( value ).doubleValue(); } catch ( Exception e ) { String message = "Couldn't convert string to number " + e.toString(); if ( !isEmpty( pattern ) ) { message += " pattern=" + pattern; } if ( !isEmpty( decimal ) ) { message += " decimal=" + decimal; } if ( !isEmpty( grouping ) ) { message += " grouping=" + grouping.charAt( 0 ); } if ( !isEmpty( currency ) ) { message += " currency=" + currency; } throw new HopValueException( message ); } }
Example 20
Source File: LegacyMoneyFormatFactory.java From template-compiler with Apache License 2.0 | 3 votes |
/** * Rely on Java for the formatting symbols (negative sign, grouping symbol, and decimal separator). * * @param locale locale * @param currency currency * @return the decimal format symbols */ private static DecimalFormatSymbols getDecimalFormatSymbols(Locale locale, Currency currency) { DecimalFormatSymbols symbols = new DecimalFormatSymbols(locale); symbols.setCurrency(currency); symbols.setCurrencySymbol(getCurrencySymbol(locale, currency)); return symbols; }