當前位置: 首頁>>代碼示例>>Java>>正文


Java NumberFormat類代碼示例

本文整理匯總了Java中java.text.NumberFormat的典型用法代碼示例。如果您正苦於以下問題:Java NumberFormat類的具體用法?Java NumberFormat怎麽用?Java NumberFormat使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。


NumberFormat類屬於java.text包,在下文中一共展示了NumberFormat類的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。

示例1: getLocalisedDouble

import java.text.NumberFormat; //導入依賴的package包/類
/**
    * Convert a string back into a Float. Assumes string was formatted using formatLocalisedNumber originally. Should
    * ensure that it is using the same locale/number format as when it was formatted. If no locale is suppied, it will
    * use the server's locale.
    *
    * Need to strip out any spaces as spaces are valid group separators in some European locales (e.g. Polish) but they
    * seem to come back from Firefox as a plain space rather than the special separating space.
    */
   public static Double getLocalisedDouble(String inputStr, Locale locale) {
String numberStr = inputStr;
if (numberStr != null) {
    numberStr = numberStr.replace(" ", "");
}
if ((numberStr != null) && (numberStr.length() > 0)) {
    Locale useLocale = locale != null ? locale : NumberUtil.getServerLocale();
    NumberFormat format = NumberFormat.getInstance(useLocale);
    ParsePosition pp = new ParsePosition(0);
    Number num = format.parse(numberStr, pp);
    if ((num != null) && (pp.getIndex() == numberStr.length())) {
	return num.doubleValue();
    }
}
throw new NumberFormatException("Unable to convert number " + numberStr + "to double using locale "
	+ locale.getCountry() + " " + locale.getLanguage());
   }
 
開發者ID:lamsfoundation,項目名稱:lams,代碼行數:26,代碼來源:NumberUtil.java

示例2: main

import java.text.NumberFormat; //導入依賴的package包/類
public static void main(String[] args) {
    NumberFormat nf = NumberFormat.getInstance();

    for (int j = 0; j < ints.length; j++) {
        String s_i = nf.format(ints[j]);
        String s_ai = nf.format(new AtomicInteger(ints[j]));
        if (!s_i.equals(s_ai)) {
            throw new RuntimeException("format(AtomicInteger " + s_ai +
                                       ") doesn't equal format(Integer " +
                                       s_i + ")");
        }
    }

    for (int j = 0; j < longs.length; j++) {
        String s_l = nf.format(longs[j]);
        String s_al = nf.format(new AtomicLong(longs[j]));
        if (!s_l.equals(s_al)) {
            throw new RuntimeException("format(AtomicLong " + s_al +
                                       ") doesn't equal format(Long " +
                                       s_l + ")");
        }
    }
}
 
開發者ID:AdoptOpenJDK,項目名稱:openjdk-jdk10,代碼行數:24,代碼來源:Bug6278616.java

示例3: attrHandler

import java.text.NumberFormat; //導入依賴的package包/類
private void attrHandler(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
    final Resources res = context.getResources();
    mDesiredMonthHeight = res.getDimensionPixelSize(R.dimen.date_picker_month_height);
    mDesiredDayOfWeekHeight = res.getDimensionPixelSize(R.dimen.date_picker_day_of_week_height);
    mDesiredDayHeight = res.getDimensionPixelSize(R.dimen.date_picker_day_height);
    mDesiredCellWidth = res.getDimensionPixelSize(R.dimen.date_picker_day_width);
    mDesiredDaySelectorRadius = res.getDimensionPixelSize(
            R.dimen.date_picker_day_selector_radius);

    // Set up accessibility components.
    mTouchHelper = new MonthViewTouchHelper(this);
    ViewCompat.setAccessibilityDelegate(this, mTouchHelper);
    ViewCompat.setImportantForAccessibility(this, ViewCompat.IMPORTANT_FOR_ACCESSIBILITY_YES);

    mLocale = res.getConfiguration().locale;
    mCalendar = Calendar.getInstance(mLocale);

    mDayFormatter = NumberFormat.getIntegerInstance(mLocale);

    updateMonthYearLabel();
    updateDayOfWeekLabels();

    initPaints(res);
}
 
開發者ID:Gericop,項目名稱:DateTimePicker,代碼行數:25,代碼來源:SimpleMonthView.java

示例4: setNumberOfAgents

import java.text.NumberFormat; //導入依賴的package包/類
/**
 * Sets the number of agents.
 * @param noAgents the new number of agents
 */
public void setNumberOfAgents(Integer noAgents) {
	
	String displayText = null;
	
	NumberFormat nf = NumberFormat.getInstance(); 
	nf.setMinimumIntegerDigits(5);  
	nf.setMaximumIntegerDigits(5); 
	nf.setGroupingUsed(false);
	
	if (noAgents==null) {
		displayText = " " + nf.format(0) + " " + Language.translate("Agenten") + " ";
	} else {
		displayText = " " + nf.format(noAgents) + " " + Language.translate("Agenten") + " ";
	}
	jLabelAgentCount.setText(displayText);
}
 
開發者ID:EnFlexIT,項目名稱:AgentWorkbench,代碼行數:21,代碼來源:SystemLoadPanel.java

示例5: getNumberFormat

import java.text.NumberFormat; //導入依賴的package包/類
/**
 * determine number of decimal places to display latitude and longitude, 
 * based on the zoom level.
 * @param zoom
 * @return
 */
public static NumberFormat getNumberFormat(double zoom) {
	NumberFormat fmt = NumberFormat.getInstance();
	if ( zoom < 16 ) {
		fmt.setMaximumFractionDigits(2);
		fmt.setMinimumFractionDigits(2);
	}
	else if ( zoom >= 16 && zoom < 256 ) {
		fmt.setMaximumFractionDigits(3);
		fmt.setMinimumFractionDigits(3);
	}
	else if ( zoom >= 256 && zoom < 4096 ) {
		fmt.setMaximumFractionDigits(4);
		fmt.setMinimumFractionDigits(4);
	}
	else if ( zoom >= 4096 && zoom < 32768 ) {
		fmt.setMaximumFractionDigits(5);
		fmt.setMinimumFractionDigits(5);
	}
	else if ( zoom >= 32768 ) {
		fmt.setMaximumFractionDigits(6);
		fmt.setMinimumFractionDigits(6);
	}
	return fmt;
}
 
開發者ID:iedadata,項目名稱:geomapapp,代碼行數:31,代碼來源:GeneralUtils.java

示例6: Currency

import java.text.NumberFormat; //導入依賴的package包/類
public Currency(int id, ConfigurationSection section, MComponentManager manager){
	this.id = id;
	this.name = manager.get(section.getString(Node.NAME.get()));
	this.single = manager.get(section.getString("SINGLE"));
	this.alias = section.getString("ALIAS");
	int type = section.getInt("FORMAT_TYPE", 0);
	this.format = (DecimalFormat) NumberFormat.getNumberInstance( type == 0 ? Locale.GERMAN : type == 1 ? Locale.ENGLISH : Locale.FRENCH);
	format.applyPattern(section.getString("FORMAT", "###,###.### "));
	this.step = section.getDouble("STEP", 0.001);
	double temp = ((int)(step*1000))/1000.0;
	if(step < 0.001 || temp != step)
		ErrorLogger.addError("Invalid step amount : " + step);
	this.min = ((int)section.getDouble("MIN", 0)/step)*step;
	this.max = ((int)section.getDouble("MAX", 9999999999.999)/step)*step;
	this.allowPay = section.getBoolean("ALLOW_PAY", false);
	this.useServer = section.getBoolean("USE_SERVER", false);
	this.booster = 1.0;
}
 
開發者ID:dracnis,項目名稱:VanillaPlus,代碼行數:19,代碼來源:Currency.java

示例7: MeterPlot

import java.text.NumberFormat; //導入依賴的package包/類
/**
 * Creates a new plot that displays the value from the supplied dataset.
 *
 * @param dataset  the dataset (<code>null</code> permitted).
 */
public MeterPlot(ValueDataset dataset) {
    super();
    this.shape = DialShape.CIRCLE;
    this.meterAngle = DEFAULT_METER_ANGLE;
    this.range = new Range(0.0, 100.0);
    this.tickSize = 10.0;
    this.tickPaint = Color.white;
    this.units = "Units";
    this.needlePaint = MeterPlot.DEFAULT_NEEDLE_PAINT;
    this.tickLabelsVisible = true;
    this.tickLabelFont = MeterPlot.DEFAULT_LABEL_FONT;
    this.tickLabelPaint = Color.black;
    this.tickLabelFormat = NumberFormat.getInstance();
    this.valueFont = MeterPlot.DEFAULT_VALUE_FONT;
    this.valuePaint = MeterPlot.DEFAULT_VALUE_PAINT;
    this.dialBackgroundPaint = MeterPlot.DEFAULT_DIAL_BACKGROUND_PAINT;
    this.intervals = new java.util.ArrayList();
    setDataset(dataset);
}
 
開發者ID:parabuild-ci,項目名稱:parabuild-ci,代碼行數:25,代碼來源:MeterPlot.java

示例8: main

import java.text.NumberFormat; //導入依賴的package包/類
public static void main(String[] args) {
	Scanner scanner = new Scanner(System.in);
	double payment = scanner.nextDouble();
	scanner.close();

	// use NumberFormat class by built-in Locale
	String us = NumberFormat.getCurrencyInstance(Locale.US).format(payment);
	String china = NumberFormat.getCurrencyInstance(Locale.CHINA).format(payment);
	String france = NumberFormat.getCurrencyInstance(Locale.FRANCE).format(payment);

	// India does not have a built-in Locale
	String india = NumberFormat.getCurrencyInstance(new Locale("en","in")).format(payment);

	System.out.println("US: " + us);
	System.out.println("India: " + india);
	System.out.println("China: " + china);
	System.out.println("France: " + france);
}
 
開發者ID:jsong00505,項目名稱:HackerRank-Studies,代碼行數:19,代碼來源:Solution.java

示例9: benchFormatFair

import java.text.NumberFormat; //導入依賴的package包/類
private static String benchFormatFair(NumberFormat nf) {
    String str = "";
    double k = 1000.0d / (double) MAX_RANGE;
    k *= k;

    double d;
    double absj;
    double jPowerOf2;
    for (int j = - MAX_RANGE; j <= MAX_RANGE; j++) {
        absj = (double) j;
        jPowerOf2 = absj * absj;
        d = k * jPowerOf2;
        if (j < 0) d = -d;
        str = nf.format(d);
    }
    return str;
}
 
開發者ID:lambdalab-mirror,項目名稱:jdk8u-jdk,代碼行數:18,代碼來源:FormatMicroBenchmark.java

示例10: finish

import java.text.NumberFormat; //導入依賴的package包/類
@Override
public void finish() {
	//write values to file

	PrintWriter fo = null;
	NumberFormat format = NumberFormat.getInstance(Locale.US);
	try {
		fo = new PrintWriter(new FileOutputStream(myFile));
		double[] values = this.myExchangeItem.getValuesAsDoubles();
		for (int i=0; i<values.length; i++) {
			String s = String.format(Locale.US, "%f", values[i]);
			fo.println(s);
		}
		fo.close();

	} catch (FileNotFoundException e) {
		e.printStackTrace();
	}
}
 
開發者ID:OpenDA-Association,項目名稱:OpenDA,代碼行數:20,代碼來源:ASCIIVectorIoObject.java

示例11: 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:MohamedSondo,項目名稱:ACE_HackerRank,代碼行數:22,代碼來源:Solution.java

示例12: test

import java.text.NumberFormat; //導入依賴的package包/類
private void test(double val) throws Exception {
    // Should behave exactly the same as standard number format
    // with no extra trailing zeros, six max trailing zeros.
    NumberFormat nf = NumberFormat.getNumberInstance();
    nf.setMinimumFractionDigits(0);
    nf.setMaximumFractionDigits(6);
    nf.setGroupingUsed(false);

    String expected = nf.format(val);

    StringBuilder sb = new StringBuilder();

    DoubleFormat.format(sb, val);

    assertEquals(expected, sb.toString());
}
 
開發者ID:awslabs,項目名稱:swage,代碼行數:17,代碼來源:DoubleFormatTest.java

示例13: formatBytes

import java.text.NumberFormat; //導入依賴的package包/類
/**
 * @return a string of the form "xxxx bytes" or "xxxxx KB" or "xxxx GB",
 * scaled as is appropriate for the current value.
 */
private String formatBytes() {
  double val;
  String scale;
  if (bytes > ONE_GB) {
    val = (double) bytes / (double) ONE_GB;
    scale = "GB";
  } else if (bytes > ONE_MB) {
    val = (double) bytes / (double) ONE_MB;
    scale = "MB";
  } else if (bytes > ONE_KB) {
    val = (double) bytes / (double) ONE_KB;
    scale = "KB";
  } else {
    val = (double) bytes;
    scale = "bytes";
  }

  NumberFormat fmt = NumberFormat.getInstance();
  fmt.setMaximumFractionDigits(MAX_PLACES);
  return fmt.format(val) + " " + scale;
}
 
開發者ID:aliyun,項目名稱:aliyun-maxcompute-data-collectors,代碼行數:26,代碼來源:PerfCounters.java

示例14: toString

import java.text.NumberFormat; //導入依賴的package包/類
@Override
public String toString() {
    DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
    NumberFormat nf = NumberFormat.getNumberInstance();
    nf.setMaximumFractionDigits(4);
    nf.setGroupingUsed(false);

    StringBuilder sb = new StringBuilder();

    sb.append("Date: ").append(date.format(formatter)).append(", ");
    sb.append("Total NAV: ").append(nf.format(totalNav)).append(", ");
    sb.append("Share price: ").append(nf.format(sharePrice)).append(", ");
    sb.append("Total shares: ").append(nf.format(getTotalShares()));

    return sb.toString();
}
 
開發者ID:ivandavidov,項目名稱:baud,代碼行數:17,代碼來源:BaudEntryDay.java

示例15: debug

import java.text.NumberFormat; //導入依賴的package包/類
public void debug() {
    if (parent != null) {
        String offset = "";
        for (CCTNode p = parent; p != null; p = p.getParent()) {
            offset += "  ";
        }
        System.out.println(offset + getNodeName() + 
                " Waits: " + getWaits() + 
                " Time: " + getTime() + 
                " " + NumberFormat.getPercentInstance().format(getTimeInPerCent()/100));
    }
    for (CCTNode ch : getChildren()) {
        if (ch instanceof LockCCTNode) {
            ((LockCCTNode) ch).debug();
        }
    }
}
 
開發者ID:apache,項目名稱:incubator-netbeans,代碼行數:18,代碼來源:LockCCTNode.java


注:本文中的java.text.NumberFormat類示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。