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


Java NumberFormat.format方法代码示例

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


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

示例1: benchFormatAllNines

import java.text.NumberFormat; //导入方法依赖的package包/类
private static String benchFormatAllNines(NumberFormat nf, boolean isCurrency) {
    String str = "";
    double[] decimaAllNines =
        {9.9993, 99.9993, 999.9993, 9999.9993, 99999.9993,
         999999.9993, 9999999.9993, 99999999.9993, 999999999.9993};
    double[] currencyAllNines =
        {9.993, 99.993, 999.993, 9999.993, 99999.993,
         999999.993, 9999999.993, 99999999.993, 999999999.993};
    double[] valuesArray = (isCurrency) ? currencyAllNines : decimaAllNines;
    double seed = 1.0 / (double) MAX_RANGE;
    double d;
    int id;
    for (int j = - MAX_RANGE; j <= MAX_RANGE; j++) {
        id = (j >=  0) ? j % 9 : -j % 9;
        if ((j & 1) == 0)
            d = valuesArray[id] + id * seed;
        else
            d = valuesArray[id] - id * seed;
        str = nf.format(d);
    }
    return str;
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:23,代码来源:FormatMicroBenchmark.java

示例2: convertByteTo

import java.text.NumberFormat; //导入方法依赖的package包/类
public String convertByteTo(long byteData){
	NumberFormat formatter = NumberFormat.getInstance();
	formatter.setMaximumFractionDigits(2);
	try {
		double kb = byteData/1024L;
		if(kb >= 1024){
			double mb = kb/1024L;
			if(mb >= 1024){
				double gb = mb/1024L;
				return formatter.format(gb) +" GB";
			}else
				return  formatter.format(mb)+" MB";
		}else{				
			return formatter.format(kb)+" KB";
		}

	} catch (Exception e) {
		// TODO: handle exception
		return String.valueOf(byteData);
	}
}
 
开发者ID:mityung,项目名称:XERUNG,代码行数:22,代码来源:Comman.java

示例3: percent2

import java.text.NumberFormat; //导入方法依赖的package包/类
public static String percent2(double p1, double p2) {
    String str;
    double p3 = p1 / p2;
    NumberFormat nf = NumberFormat.getPercentInstance();
    nf.setMinimumFractionDigits(0);
    str = nf.format(p3);
    return str;
}
 
开发者ID:snowwolf10285,项目名称:PicShow-zhaipin,代码行数:9,代码来源:DeviceUtils.java

示例4: setDiliverPer

import java.text.NumberFormat; //导入方法依赖的package包/类
public static String setDiliverPer(int diliverNum, int queryMailNum) {
	NumberFormat numberFormat = NumberFormat.getInstance();
	// 设置精确到小数点后2位
	numberFormat.setMaximumFractionDigits(2);
	String result = numberFormat.format((float) diliverNum / (float) queryMailNum * 100);
	return result;
}
 
开发者ID:babymm,项目名称:mumu,代码行数:8,代码来源:DiliverPerUtil.java

示例5: onCreate

import java.text.NumberFormat; //导入方法依赖的package包/类
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_consumption);
    ActionBar actionBar = getSupportActionBar();
    if (actionBar != null)
        actionBar.setDisplayHomeAsUpEnabled(true);

    consumptionPerDay = getIntent().getFloatExtra(CONSUMPTION_PER_DAY, 0);

    consumptionTv = findViewById(R.id.consumption_tv);
    float consumptionPerYear = consumptionPerDay * NUMBER_OF_DAYS_PER_YEAR;
    NumberFormat format = NumberFormat.getInstance();
    String consumptionStr = format.format(consumptionPerYear) + getString(R.string.energy_unit);
    consumptionTv.setText(consumptionStr);

    float cost;
    if (consumptionPerDay > FIRST_TIER_MAX)
        cost = FIRST_TIER_MAX * ENERGY_COST_FIRST_TIER + (consumptionPerDay - FIRST_TIER_MAX) * ENERGY_COST_SECOND_TIER;
    else
        cost = consumptionPerDay * ENERGY_COST_FIRST_TIER;
    cost = cost * NUMBER_OF_DAYS_PER_YEAR;

    NumberFormat moneyFormat = NumberFormat.getCurrencyInstance();
    String costStr = moneyFormat.format(cost);
    costTv = findViewById(R.id.cost_tv);
    costTv.setText(costStr);
}
 
开发者ID:Sonphil,项目名称:PhantomConsumptionCalculator,代码行数:29,代码来源:ConsumptionActivity.java

示例6: formatOnPattern

import java.text.NumberFormat; //导入方法依赖的package包/类
private static void formatOnPattern(NumberFormat nf, String pattern,
        double number, String expected) {

    if (nf instanceof DecimalFormat) {
        ((DecimalFormat) nf).applyPattern(pattern);
    }

    String formatted = nf.format(number);
    if (!formatted.equals(expected)) {
        throw new RuntimeException("[FAILED: Unable to format the number"
                + " based on the pattern: '" + pattern + "', Expected : '"
                + expected + "', Found: '" + formatted + "']");
    }
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:15,代码来源:Bug6609740.java

示例7: sendSuccessNotification

import java.text.NumberFormat; //导入方法依赖的package包/类
void sendSuccessNotification(double spent, String bought) {
    NumberFormat formatter = new DecimalFormat("#0.00");
    String spentString = formatter.format(spent);

    trackBuy(bought, spentString);

    sendNotification("Bought " + bought + " " + preferences.getCryptoCurrency(), "Paid " + spentString + " " + preferences.getBaseCurrency(), -1);
}
 
开发者ID:aomega08,项目名称:Keep-HODLing,代码行数:9,代码来源:BuyEvent.java

示例8: test

import java.text.NumberFormat; //导入方法依赖的package包/类
private static void test() {

        File root = new File("C:\\");
        System.out.println("Root Exists: " + root.exists());
        System.out.println("Root Absolute Path: " + root.getAbsolutePath());
        System.out.println("Root Is Directory?: " + root.isDirectory());

        FileSystemView fileSystemView = FileSystemView.getFileSystemView();
        NumberFormat nf = NumberFormat.getNumberInstance();

        int iMax = 50000;
        long lastPercentFinished = 0L;
        for (int i = 0; i < iMax; i++) {

            long percentFinished = Math.round(((i * 1000d) / (double) iMax));

            if (lastPercentFinished != percentFinished) {
                double pf = ((double) percentFinished) / 10d;
                String pfMessage = String.valueOf(pf) + " % (" + i + "/" + iMax + ")";

                long totalMemory = Runtime.getRuntime().totalMemory() / 1024;
                long freeMemory = Runtime.getRuntime().freeMemory() / 1024;
                long maxMemory = Runtime.getRuntime().maxMemory() / 1024;
                String memMessage = "[Memory Used: " + nf.format(totalMemory) +
                                    " kb Free=" + nf.format(freeMemory) +
                                    " kb Max: " + nf.format(maxMemory) + " kb]";

                System.out.println(pfMessage + " " + memMessage);
                lastPercentFinished = percentFinished;
            }

            boolean floppyDrive = fileSystemView.isFloppyDrive(root);
            boolean computerNode = fileSystemView.isComputerNode(root);

            // "isDrive()" seems to be the painful method...
            boolean drive = fileSystemView.isDrive(root);
        }
    }
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:39,代码来源:FileSystemViewMemoryLeak.java

示例9: getAllCurrencyNoWallet

import java.text.NumberFormat; //导入方法依赖的package包/类
public static String getAllCurrencyNoWallet(EntityPlayer player) {
	float currencyTotal = 0;

	for (int i = 0; i < player.inventory.getSizeInventory(); i++) {
		if (player.inventory.getStackInSlot(i) != ItemStack.EMPTY && player.inventory.getStackInSlot(i).getItem() instanceof ItemMoneyBase) {
			ItemMoneyBase money = (ItemMoneyBase) player.inventory.getStackInSlot(i).getItem();
			currencyTotal += (money.getValue() * player.inventory.getStackInSlot(i).getCount());
		}
	}

	NumberFormat fmt = NumberFormat.getCurrencyInstance(Locale.US);
	return fmt.format(currencyTotal);
}
 
开发者ID:Zundrel,项目名称:Never-Enough-Currency,代码行数:14,代码来源:CurrencyUtils.java

示例10: getMaxPossibleBuffer

import java.text.NumberFormat; //导入方法依赖的package包/类
public static String getMaxPossibleBuffer(Profile profile) {
    double minRatio = profile.response.stats.requiredRatio;
    double dl = bytesToGibiBytesDouble(profile.response.stats.downloaded);
    double ul = bytesToGibiBytesDouble(profile.response.stats.uploaded);

    double maxDl = ul / minRatio;
    double buffer = maxDl - dl;
    NumberFormat formatter = new DecimalFormat("#0.00");
    return formatter.format(buffer);
}
 
开发者ID:stuxo,项目名称:REDAndroid,代码行数:11,代码来源:Calculator.java

示例11: formatOutputTestObject

import java.text.NumberFormat; //导入方法依赖的package包/类
static void formatOutputTestObject(NumberFormat nf,
                                   Object someNumber,
                                   String tiePosition,
                                   String inputDigits,
                                   String expectedOutput) {

    int mfd = nf.getMaximumFractionDigits();
    RoundingMode rm = nf.getRoundingMode();
    String result = nf.format(someNumber);

    if (!result.equals(expectedOutput)) {
        System.out.println();
        System.out.println("========================================");
        System.out.println("***Failure : error formatting value from string : " +
                           inputDigits);
        System.out.println("NumberFormat pattern is  : " +
                           ((DecimalFormat ) nf).toPattern());
        System.out.println("Maximum number of fractional digits : " + mfd);
        System.out.println("Fractional rounding digit : " + (mfd + 1));
        System.out.println("Position of value relative to tie : " + tiePosition);
        System.out.println("Rounding Mode : " + rm);
        System.out.println("Number self output representation: " + someNumber);
        System.out.println(
           "Error. Formatted result different from expected." +
           "\nExpected output is : \"" + expectedOutput + "\"" +
           "\nFormated output is : \"" + result + "\"");
        System.out.println("========================================");
        System.out.println();

        errorCounter++;
        allPassed = false;
    } else {
        testCounter++;
        System.out.print("Success. Number input :" + inputDigits);
        System.out.print(", rounding : " + rm);
        System.out.print(", fract digits : " + mfd);
        System.out.print(", tie position : " + tiePosition);
        System.out.println(", expected : " + expectedOutput);
    }
}
 
开发者ID:lambdalab-mirror,项目名称:jdk8u-jdk,代码行数:41,代码来源:TieRoundingTest.java

示例12: getDateString

import java.text.NumberFormat; //导入方法依赖的package包/类
private static String getDateString() {
	String out = "";
	NumberFormat formatter = new DecimalFormat("00");
	NumberFormat formatter1 = new DecimalFormat("000");

	int hours = Calendar.getInstance().get(Calendar.HOUR);
	int minutes = Calendar.getInstance().get(Calendar.MINUTE);
	int seconds = Calendar.getInstance().get(Calendar.SECOND);
	int msec = Calendar.getInstance().get(Calendar.MILLISECOND);

	out = formatter.format(hours)+":"+formatter.format(minutes)+":"+formatter.format(seconds)+":"+formatter1.format(msec);
	return out;
}
 
开发者ID:ZetzmannM,项目名称:CGL,代码行数:14,代码来源:StatusPrinter.java

示例13: setFileSizeSave

import java.text.NumberFormat; //导入方法依赖的package包/类
public void setFileSizeSave(long size){
	NumberFormat nf=NumberFormat.getNumberInstance() ; 
	nf.setMaximumFractionDigits(2);
	if(size<1024){
		fileSize =nf.format(size) +"B";
	}else if(size<1024*1024){
		this.fileSize =nf.format(size/1024.0) + "KB";
	}else if(size<1024*1024*1024){
		this.fileSize = nf.format(size/1024.0 * 1024) + "MB";
	}else if(size<1024*1024*1024*1024){
		this.fileSize = nf.format(size/1024.0 * 1024 * 1024) + "GB";
	}	
}
 
开发者ID:ranji1221,项目名称:clemon,代码行数:14,代码来源:BackupDatabaseInfo.java

示例14: benchFormatSmallIntegral

import java.text.NumberFormat; //导入方法依赖的package包/类
private static String benchFormatSmallIntegral(NumberFormat nf) {
    String str = "";
    for (int j = - MAX_RANGE; j <= MAX_RANGE; j++)
        str = nf.format(((double) j) / 1000.0d);
    return str;
}
 
开发者ID:lambdalab-mirror,项目名称:jdk8u-jdk,代码行数:7,代码来源:FormatMicroBenchmark.java

示例15: formatOutputTestDouble

import java.text.NumberFormat; //导入方法依赖的package包/类
static void formatOutputTestDouble(NumberFormat nf,
                                   double doubleToTest,
                                   String tiePosition,
                                   String inputDigits,
                                   String expectedOutput) {

    int mfd = nf.getMaximumFractionDigits();
    RoundingMode rm = nf.getRoundingMode();
    String result = nf.format(doubleToTest);

    if (!result.equals(expectedOutput)) {
        System.out.println();
        System.out.println("========================================");
        System.out.println("***Failure : error formatting value from string : " +
                           inputDigits);
        System.out.println("NumberFormat pattern is  : " +
                           ((DecimalFormat ) nf).toPattern());
        System.out.println("Maximum number of fractional digits : " + mfd);
        System.out.println("Fractional rounding digit : " + (mfd + 1));
        System.out.println("Position of value relative to tie : " + tiePosition);
        System.out.println("Rounding Mode : " + rm);
        System.out.println("BigDecimal output : " +
                           new BigDecimal(doubleToTest).toString());
        System.out.println("FloatingDecimal output : " + doubleToTest);
        System.out.println(
           "Error. Formatted result different from expected." +
           "\nExpected output is : \"" + expectedOutput + "\"" +
           "\nFormated output is : \"" + result + "\"");
        System.out.println("========================================");
        System.out.println();

        errorCounter++;
        allPassed = false;
    } else {
        testCounter++;
        System.out.println("\nSuccess for double value : " + doubleToTest + " :");
        System.out.println(" Input digits :" + inputDigits +
                           ", BigDecimal value : " +
                           new BigDecimal(doubleToTest).toString());
        System.out.print(" Rounding mode: " + rm);
        System.out.print(", fract digits : " + mfd);
        System.out.print(", position : " + tiePosition + " tie");
        System.out.print(", result : " + result);
        System.out.println(", expected : " + expectedOutput);
    }
}
 
开发者ID:lambdalab-mirror,项目名称:jdk8u-jdk,代码行数:47,代码来源:TieRoundingTest.java


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