当前位置: 首页>>代码示例>>Java>>正文


Java NumberFormat.getCurrencyInstance方法代码示例

本文整理汇总了Java中java.text.NumberFormat.getCurrencyInstance方法的典型用法代码示例。如果您正苦于以下问题:Java NumberFormat.getCurrencyInstance方法的具体用法?Java NumberFormat.getCurrencyInstance怎么用?Java NumberFormat.getCurrencyInstance使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在java.text.NumberFormat的用法示例。


在下文中一共展示了NumberFormat.getCurrencyInstance方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: main

import java.text.NumberFormat; //导入方法依赖的package包/类
public static void main(String[] args) {
    /* Save input */
    Scanner scan = new Scanner(System.in);
    double payment = scanner.nextDouble();
    scan.close();

    /* Create custom Locale for India - I used the "IANA Language Subtag Registry" to find India's country code */
    Locale indiaLocale = new Locale("en", "IN");

    /* Create NumberFormats using Locales */
    NumberFormat us     = NumberFormat.getCurrencyInstance(Locale.US);
    NumberFormat india  = NumberFormat.getCurrencyInstance(indiaLocale);
    NumberFormat china  = NumberFormat.getCurrencyInstance(Locale.CHINA);
    NumberFormat france = NumberFormat.getCurrencyInstance(Locale.FRANCE);

    /* Print output */        
    System.out.println("US: "     + us.format(payment));
    System.out.println("India: "  + india.format(payment));
    System.out.println("China: "  + china.format(payment));
    System.out.println("France: " + france.format(payment));
}
 
开发者ID:rshaghoulian,项目名称:HackerRank_solutions,代码行数:22,代码来源:Solution.java

示例2: WettDialog

import java.text.NumberFormat; //导入方法依赖的package包/类
/**
    * Creates a new WettDialog and displays the given array of snails.
    * @param parent The Form opening this dialog
    * @param schneggen The array of snails to display
    * @param wettbueroFactor The factor the Wettbuero uses.
    */
   public WettDialog(Frame parent, ArrayList<Rennschnecke> schneggen, double wettbueroFactor) {
       super(parent, true);
       initComponents();
       NumberFormat format = NumberFormat.getCurrencyInstance();
format.setMinimumFractionDigits(2);
format.setMaximumFractionDigits(2);
       NumberFormatter nf = new NumberFormatter(format);
       nf.setMinimum(0.02);
// The maximum bet value is, well, pretty high.
       nf.setMaximum(Double.MAX_VALUE / wettbueroFactor);
       nf.setAllowsInvalid(false);
       nf.setCommitsOnValidEdit(true);
       nf.setOverwriteMode(false);
       einsatzInput.setFormatterFactory(
               new DefaultFormatterFactory(nf)
       );
       result = null;
       snailList.setModel(new DefaultListModel<>());
       snailList.setListData(schneggen.toArray(new Rennschnecke[schneggen.size()]));
   }
 
开发者ID:Follpvosten,项目名称:schneckenrennen,代码行数:27,代码来源:WettDialog.java

示例3: format

import java.text.NumberFormat; //导入方法依赖的package包/类
/**
 * Formats Money into a human readable currency string.
 *
 * @param moneyOrNull the money to format, or null
 * @param locale the {@link Locale} to format for
 * @return a formatted money string, or null if moneyOrNull is null
 */
public static String format(Money moneyOrNull, Locale locale) {
  if (moneyOrNull == null) {
    return null;
  }

  // Convert the currency specified in the proto to a Currency object.
  Currency currency = Currency.getInstance(moneyOrNull.getCurrency().toString());

  // Create a formatter that uses the currency.
  NumberFormat formatter = NumberFormat.getCurrencyInstance(locale);
  formatter.setCurrency(currency);
  formatter.setMaximumFractionDigits(currency.getDefaultFractionDigits());

  if (currency.getDefaultFractionDigits() == 0) {
    // For locales that do not support fractional amounts, use the amount as is. For example,
    // Japan has no concept of "cents". The base unit of currency is 1 Yen, and currency is always
    // measured in Yen.
    return formatter.format(moneyOrNull.getAmount());
  } else {
    // For locales that support fractional amounts, divide by 100 to get the local equivalent of
    // "dollars". For example, the amount in USD is represented as cents. We devide by 100 to get
    // US dollars.
    return formatter.format(moneyOrNull.getAmount() / 100.0);
  }
}
 
开发者ID:square,项目名称:catalog-api-demo,代码行数:33,代码来源:Moneys.java

示例4: main

import java.text.NumberFormat; //导入方法依赖的package包/类
public static void main (String argv[] )  {
    Locale reservedLocale = Locale.getDefault();
    try {
        String expectedCurrencyPattern = "\u00A4 #.##0,00";

        Locale locale = new Locale ("pt", "BR");
        Locale.setDefault(locale);

        DecimalFormat formatter =
            (DecimalFormat) NumberFormat.getCurrencyInstance(locale);

        if (formatter.toLocalizedPattern().equals(
                    expectedCurrencyPattern)) {
            System.out.println ("Passed.");
        } else {
             System.out.println ("Failed Currency pattern." +
                    "  Expected:  " + expectedCurrencyPattern +
                    "  Received:  " + formatter.toLocalizedPattern() );
             throw new RuntimeException();

        }
    } finally {
        // restore the reserved locale
        Locale.setDefault(reservedLocale);
    }
}
 
开发者ID:lambdalab-mirror,项目名称:jdk8u-jdk,代码行数:27,代码来源:Bug4651568.java

示例5: onItemSelected

import java.text.NumberFormat; //导入方法依赖的package包/类
@Override
public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
    // TODO Auto-generated method stub

    //Currency Format
    NumberFormat formatter = NumberFormat.getCurrencyInstance();

    if (arg0.getId() == R.id.book_spinner) {
        //Save the choice as a string
        Object item = arg0.getItemAtPosition(arg2);
        String title = item.toString();

        //Loop through the available books list to see if the title matches
        for(int i=0; i<availableBooks.size(); i++){
            //if it matches change the text of the details
            if(availableBooks.get(i).getTitle().equals(title)){
                TextView main = (TextView) findViewById(R.id.main);

                main.setText("");

                main.append("Title: " + availableBooks.get(i).getTitle() + "\n");
                main.append("Author: " + availableBooks.get(i).getAuthor() + "\n");
                main.append("ISBN: " + availableBooks.get(i).getIsbn() + "\n");
                main.append("Cost Per Hour: " + formatter.format(availableBooks.get(i).getPrice()));
                cost = availableBooks.get(i).getPrice();

            }
        }


        //get the book in the array with this Title
    }

}
 
开发者ID:Mikerr1111,项目名称:Library-System-Android,代码行数:35,代码来源:BookResults.java

示例6: formatCurrency

import java.text.NumberFormat; //导入方法依赖的package包/类
public static String formatCurrency(long amount) {
    if (amount == 0) {
        return "N/A";
    }

    NumberFormat numberFormat = NumberFormat.getCurrencyInstance(Locale.getDefault());
    numberFormat.setMinimumFractionDigits(0);
    return numberFormat.format(amount);
}
 
开发者ID:scaffeinate,项目名称:Inflix,代码行数:10,代码来源:DisplayUtils.java

示例7: bind

import java.text.NumberFormat; //导入方法依赖的package包/类
@Override
public void bind(LinearLayout view)
{
    ButterKnife.bind(this, view);
    NumberFormat numberFormat = NumberFormat.getCurrencyInstance();
    tvPrice.setText(numberFormat.format(price));
    tvReleaseName.setText(releaseName);
}
 
开发者ID:jbmlaird,项目名称:DiscogsBrowser,代码行数:9,代码来源:OrderReleaseModel.java

示例8: formatCurrency

import java.text.NumberFormat; //导入方法依赖的package包/类
@Koan
public void formatCurrency() {
    float someAmount = 442.23f; // Don't use floats for money in real life. Really. It's a bad idea.
    Locale locBR = new Locale("pt", "BR");
    NumberFormat nf = NumberFormat.getCurrencyInstance(locBR);
    assertEquals(nf.format(someAmount), __);
}
 
开发者ID:linbin0107,项目名称:koans,代码行数:8,代码来源:AboutLocale.java

示例9: init

import java.text.NumberFormat; //导入方法依赖的package包/类
@Override
public void init() throws OperatorException {
	int localeIndex = getParameterAsInt(PARAMETER_LOCALE);
	Locale selectedLocale = Locale.US;
	if ((localeIndex >= 0) && (localeIndex < availableLocales.size())) {
		selectedLocale = availableLocales.get(getParameterAsInt(PARAMETER_LOCALE));
	}

	int formatType = getParameterAsInt(PARAMETER_FORMAT_TYPE);
	switch (formatType) {
		case FORMAT_TYPE_NUMBER:
			this.numberFormat = NumberFormat.getNumberInstance(selectedLocale);
			break;
		case FORMAT_TYPE_INTEGER:
			this.numberFormat = NumberFormat.getIntegerInstance(selectedLocale);
			break;
		case FORMAT_TYPE_CURRENCY:
			this.numberFormat = NumberFormat.getCurrencyInstance(selectedLocale);
			break;
		case FORMAT_TYPE_PERCENT:
			this.numberFormat = NumberFormat.getPercentInstance(selectedLocale);
			break;
		case FORMAT_TYPE_PATTERN:
			String formatString = getParameterAsString(PARAMETER_PATTERN);
			// the following line only works for Java Versions >= 6
			// this.numberFormat = new DecimalFormat(formatString,
			// DecimalFormatSymbols.getInstance(selectedLocale));
			this.numberFormat = new DecimalFormat(formatString, new DecimalFormatSymbols(selectedLocale));
			break;
	}

	this.numberFormat.setGroupingUsed(getParameterAsBoolean(PARAMETER_USE_GROUPING));
}
 
开发者ID:transwarpio,项目名称:rapidminer,代码行数:34,代码来源:NumericToFormattedNominal.java

示例10: formatCurrency

import java.text.NumberFormat; //导入方法依赖的package包/类
/**
 * 使用默认方式显示货币:
 * 例如:¥12,345.46 默认保留2位小数,四舍五入
 *
 * @param d double
 * @return String
 */
public static String formatCurrency(double d) {
    String s = "";
    try {
        DecimalFormat nf = (DecimalFormat) NumberFormat.getCurrencyInstance(Locale.CHINA);
        s = nf.format(d);
    } catch (Exception e) {
        e.printStackTrace();
        return "" + d;
    }
    return s;
}
 
开发者ID:paterWang,项目名称:EasyReader,代码行数:19,代码来源:BaseTools.java

示例11: convertOutbound

import java.text.NumberFormat; //导入方法依赖的package包/类
public OutboundVariable convertOutbound(Object data, OutboundContext outctx)
		throws MarshallException {
	Double out = 0.0;
	// Error out if an unsupported class is passed
	if (!(data instanceof Currency)) {
		log.warn("Unsupported input. Class=" + data.getClass() + ", data="
				+ data);
		throw new MarshallException(data.getClass());
	}
	String currency = data.toString();
	if (currency != null && currency != "") {
		String layout = CurrencyFieldMetaData.getCurrencyFormat();
		NumberFormat fmt;
		if (LocaleContext.getLocale() != null) {
			fmt = NumberFormat.getCurrencyInstance(LocaleContext
					.getLocale());
		} else {
			fmt = NumberFormat.getCurrencyInstance();
		}
		fmt.setCurrency(java.util.Currency.getInstance(Currency.USD));
		if (layout != null && layout.length() > 0
				&& fmt instanceof DecimalFormat) {
			((DecimalFormat) fmt).applyPattern(layout);
		}
		try {
			out = Double.valueOf((fmt.parse(currency)).doubleValue());
			currency = out.toString();
		} catch (ParseException e) {
		}
	}
	return new SimpleOutboundVariable(currency, outctx, true);
}
 
开发者ID:jaffa-projects,项目名称:jaffa-framework,代码行数:33,代码来源:CurrencyConverter.java

示例12: getNumberFormat

import java.text.NumberFormat; //导入方法依赖的package包/类
@Override
protected NumberFormat getNumberFormat(Locale locale) {
	DecimalFormat format = (DecimalFormat) NumberFormat.getCurrencyInstance(locale);
	format.setParseBigDecimal(true);
	format.setMaximumFractionDigits(this.fractionDigits);
	format.setMinimumFractionDigits(this.fractionDigits);
	if (this.roundingMode != null && roundingModeOnDecimalFormat) {
		format.setRoundingMode(this.roundingMode);
	}
	if (this.currency != null) {
		format.setCurrency(this.currency);
	}
	return format;
}
 
开发者ID:lamsfoundation,项目名称:lams,代码行数:15,代码来源:CurrencyFormatter.java

示例13: main

import java.text.NumberFormat; //导入方法依赖的package包/类
public static void main(String ... ags)throws IOException
{
	BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
	double pay =Double.parseDouble(in.readLine());

	NumberFormat nf = null;
	String str;

	//For US 
	nf = NumberFormat.getCurrencyInstance(Locale.US);
	str=nf.format(pay);
	System.out.println("US: "+str);

	//For India
	Locale india = new Locale("en","IN");
	nf = NumberFormat.getCurrencyInstance(india);
	str=nf.format(pay);
	System.out.println("India: "+str);

	//For China
	nf = NumberFormat.getCurrencyInstance(Locale.CHINA);
	str=nf.format(pay);
	System.out.println("China: "+str);

	//For France
	nf = NumberFormat.getCurrencyInstance(Locale.FRANCE);
	str=nf.format(pay);
	System.out.println("France: "+str);	
}
 
开发者ID:hell-sing,项目名称:hacker-rank,代码行数:30,代码来源:Java_Currency_Formatter.java

示例14: format

import java.text.NumberFormat; //导入方法依赖的package包/类
public String format(Double doubleToFormat)
{
    NumberFormat numberFormat = NumberFormat.getCurrencyInstance();
    return numberFormat.format(doubleToFormat);
}
 
开发者ID:jbmlaird,项目名称:DiscogsBrowser,代码行数:6,代码来源:NumberFormatWrapper.java

示例15: testCurrencyFormat

import java.text.NumberFormat; //导入方法依赖的package包/类
void testCurrencyFormat() {
    NumberFormat format = NumberFormat.getCurrencyInstance(Locale.US);
    TestFormattedTextField ftf = create(format);
    ftf.setValue(56L);

    System.err.println("Testing NumberFormat.getCurrencyInstance(Locale.US)");

    // test inserting individual characters
    ftf.test(1, 0, "1", 16L);
    ftf.test(3, 0, "2", 162L);
    ftf.test(2, 0, "0", 102L);

    // test inserting several characters at once - e.g. from clipboard
    ftf.test(1, 0, "1024", 1024L);
    ftf.test(4, 0, "333", 10333L);
    ftf.test(7, 0, "77", 1033377L);
    ftf.test(5, 0, "99", 1039977L);
    ftf.test(7, 0, "00", 1039007L);

    // test inserting strings that contain some formatting
    ftf.test(1, 0, "2,2", 229007L);
    ftf.test(3, 0, "2,2", 22227L);
    ftf.test(4, 0, "2,2", 2222L);
    ftf.test(6, 0, "33,33", 22223333L);

    // test delete
    ftf.test(1, 0, DELETE, 2223333L);
    ftf.test(10, 0, DELETE, 2223333L);
    ftf.test(5, 0, DELETE, 222333L);
    ftf.test(5, 0, DELETE, 22233L);

    // test backspace
    ftf.test(1, 0, BACKSPACE, 22233L);
    ftf.test(7, 0, BACKSPACE, 2223L);
    ftf.test(4, 0, BACKSPACE, 223L);
    ftf.test(2, 0, BACKSPACE, 23L);

    // test replacing selection
    ftf.test(1, 1, "555", 5553L);
    ftf.test(4, 2, "555", 55555L);
    ftf.test(2, 3, "1", 5155L);
    ftf.test(3, 2, "6", 565L);
    ftf.test(1, 3, "111222333444555", 111222333444555L);

    // test deleting selection
    ftf.test(1, 2, DELETE, 1222333444555L);
    ftf.test(1, 3, BACKSPACE, 22333444555L);
    ftf.test(13, 2, DELETE, 223334445L);
    ftf.test(10, 2, BACKSPACE, 2233344L);
    ftf.test(4, 4, DELETE, 2244L);
    ftf.test(1, 4, BACKSPACE, 4L);
}
 
开发者ID:lambdalab-mirror,项目名称:jdk8u-jdk,代码行数:53,代码来源:Test6462562.java


注:本文中的java.text.NumberFormat.getCurrencyInstance方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。