本文整理汇总了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());
}
示例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 + ")");
}
}
}
示例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);
}
示例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);
}
示例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;
}
示例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;
}
示例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);
}
示例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);
}
示例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;
}
示例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();
}
}
示例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));
}
示例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());
}
示例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;
}
示例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();
}
示例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();
}
}
}