本文整理汇总了Java中java.text.DecimalFormat.applyPattern方法的典型用法代码示例。如果您正苦于以下问题:Java DecimalFormat.applyPattern方法的具体用法?Java DecimalFormat.applyPattern怎么用?Java DecimalFormat.applyPattern使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类java.text.DecimalFormat
的用法示例。
在下文中一共展示了DecimalFormat.applyPattern方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: write
import java.text.DecimalFormat; //导入方法依赖的package包/类
public void write(PrintWriter printer, TimeSeries series) {
Locale locale = new Locale("en", "US");
DecimalFormat formatter = (DecimalFormat)NumberFormat.getNumberInstance(locale);
formatter.applyPattern("0.0000######");
printer.print(String.format(header, series.getProperty("sensorid"), series.getLocation(), series.getProperty("position"), series.getSource(), series.getQuantityId(), series.getUnitId(), series.getProperty("use") ));
final double times[] = series.getTimesRef();
final double values[] = series.getValuesRef();
//TODO: detect order on read and use this order
printer.println("datetime;value");
String dateString;
for (int i = 0; i < times.length; i++) {
dateString = TimeUtils.mjdToString( times[i],this.datePattern);
printer.println( dateString + this.delimiter + formatter.format(values[i]) );
}
printer.flush();
}
示例2: MakeOrderNum
import java.text.DecimalFormat; //导入方法依赖的package包/类
/**
* 订单单号生成:17位时间戳,由5位商品id,3位商品类型id,7为用户id,
*
* @param uid 用户id
* @param pid 商品id
* @param ptid 商品类型id
* @return 32位纯数字字符串
*/
public static String MakeOrderNum(Integer uid,Integer ptid) {
DecimalFormat df = new DecimalFormat("000");
Random random = new Random();
String finllNum = null;
String ptidstr=null;
String struid = null;
String pidstr=null;
if (ptid != null) {
ptidstr = df.format(ptid);
} else {
ptidstr = df.format(random.nextInt(100));
}
df.applyPattern("0000000");
if (uid != null) {
struid = df.format(uid);
} else {
struid = df.format(random.nextInt(1000000));
}
long nowLong = Long.parseLong(new SimpleDateFormat("yyyyMMddHHmmssSSS").format(new Date()));
finllNum = new StringBuffer().append(nowLong).append(ptidstr).append(struid).toString();
return finllNum;
}
示例3: FormatNumber
import java.text.DecimalFormat; //导入方法依赖的package包/类
/**
* Helper method used to formats given number into string according to default rules.
* @param d bouble to be converted
* @return string representation of given number
*/
protected String FormatNumber(double d) {
DecimalFormat nf = new DecimalFormat();
String ret;
// If module of number is greater than 1e4 or lesser than 1e-4 uses exponential notation
if (Math.abs(d) >= 1e-4 && Math.abs(d) <= 1e4 || d == 0) {
nf.applyPattern("#.####");
ret = nf.format(d);
if (ret.length() > 7) {
ret = ret.substring(0, 6);
}
} else {
nf.applyPattern("0.00E00");
ret = nf.format(d);
}
return ret;
}
示例4: parse
import java.text.DecimalFormat; //导入方法依赖的package包/类
/**
* Convert the specified locale-sensitive input object into an output
* object of the specified type.
*
* @param value The input object to be converted
* @param pattern The pattern is used for the convertion
* @return The converted value
*
* @throws org.apache.commons.beanutils.ConversionException if conversion
* cannot be performed successfully
* @throws ParseException if an error occurs parsing a String to a Number
*/
@Override
protected Object parse(final Object value, final String pattern) throws ParseException {
if (value instanceof Number) {
return value;
}
// Note that despite the ambiguous "getInstance" name, and despite the
// fact that objects returned from this method have the same toString
// representation, each call to getInstance actually returns a new
// object.
final DecimalFormat formatter = (DecimalFormat) DecimalFormat.getInstance(locale);
// if some constructors default pattern to null, it makes only sense
// to handle null pattern gracefully
if (pattern != null) {
if (locPattern) {
formatter.applyLocalizedPattern(pattern);
} else {
formatter.applyPattern(pattern);
}
} else {
log.debug("No pattern provided, using default.");
}
return formatter.parse((String) value);
}
示例5: apply
import java.text.DecimalFormat; //导入方法依赖的package包/类
@Override void apply(DecimalFormat decimalFormat) {
/* To switch to using codes from symbols, we replace each single occurrence of the
* currency-sign character with two such characters in a row.
* We also insert a space character between every occurence of this character and an
* adjacent numerical digit or negative sign (that is, between the currency-sign and
* the signed-number). */
decimalFormat.applyPattern(
negify(decimalFormat.toPattern()).replaceAll("¤","¤¤").
replaceAll("([#0.,E-])¤¤","$1 ¤¤").
replaceAll("¤¤([0#.,E-])","¤¤ $1")
);
}
示例6: FormatNumber
import java.text.DecimalFormat; //导入方法依赖的package包/类
/**
* Helper method used to formats given number into string according to default rules.
* @param d double to be converted
* @return string representation of given number
*/
protected String FormatNumber(double d) {
DecimalFormat nf = new DecimalFormat();
nf.setDecimalFormatSymbols(new DecimalFormatSymbols(Locale.ENGLISH));
String ret = null;
// If module of number is greater than 1e-3 or lesser than 1e3 uses ordinary notation
if (Math.abs(d) > 1e-3 && Math.abs(d) < 1e3 || d == 0) {
nf.applyPattern("#.###");
ret = nf.format(d);
} else {
nf.applyPattern("0.00E00");
ret = nf.format(d);
}
return ret;
}
示例7: FormatNumber
import java.text.DecimalFormat; //导入方法依赖的package包/类
/**
* Helper method used to formats given number into string according to default rules.
* @param d double to be converted
* @return string representation of given number
*/
protected String FormatNumber(double d) {
DecimalFormat nf = new DecimalFormat();
nf.setDecimalFormatSymbols(new DecimalFormatSymbols(Locale.ENGLISH));
String ret = null;
// If module of number is greater than 1e-4 or lesser than 1e4 uses ordinary notation
if (Math.abs(d) > 1e-4 && Math.abs(d) < 1e4 || d == 0) {
nf.applyPattern("#.####");
ret = nf.format(d);
} else {
nf.applyPattern("0.00E00");
ret = nf.format(d);
}
return ret;
}
示例8: write
import java.text.DecimalFormat; //导入方法依赖的package包/类
public void write(PrintWriter printer, TimeSeries series) {
Locale locale = new Locale("en", "US");
DecimalFormat formatter = (DecimalFormat)NumberFormat.getNumberInstance(locale);
formatter.applyPattern("0.0######E000");
// now the data
final double times[] = series.getTimesRef();
final double values[] = series.getValuesRef();
for (int i = 0; i < times.length; i++) {
String line = formatter.format((times[i] - referenceDateInMjd)/factorToMjd) + " " + formatter.format(values[i]);
printer.println(line);
}
printer.flush();
}
示例9: TrackPresenterImpl
import java.text.DecimalFormat; //导入方法依赖的package包/类
public TrackPresenterImpl(){
mDistanceFormater = (DecimalFormat) NumberFormat.getInstance();
mDistanceFormater.setMinimumFractionDigits(2);
mDistanceFormater.setMaximumFractionDigits(2);
mTimeFormater = (DecimalFormat) DecimalFormat.getInstance();
mTimeFormater.applyPattern("00");
}
示例10: getValueToDisplay
import java.text.DecimalFormat; //导入方法依赖的package包/类
/**
* Returns the formatted value as a String, which is displayed for a given
* price of type BigDecimal. The formatting used takes into account the
* given locale, and optionally a grouping separator based on the locale.
*
* @param price
* the price as a BigDecimal to be formatted.
* @param useGrouping
* a flag indicating whether a grouping for the formatting will
* be used or not.
* @param locale
* the locale to use for the formatting.
* @return the displayed price formatted value as a String.
*/
public String getValueToDisplay(BigDecimal price, boolean useGrouping,
Locale locale) {
DecimalFormat nf = new DecimalFormat();
nf.setDecimalFormatSymbols(new DecimalFormatSymbols(locale));
nf.setGroupingUsed(useGrouping);
nf.setMinimumFractionDigits(MINIMUM_FRACTION_DIGIT);
if (useGrouping) {
nf.applyPattern(PriceConverter.FORMAT_PATTERN_WITH_GROUPING);
} else {
nf.applyPattern(PriceConverter.FORMAT_PATTERN_WITHOUT_GROUPING);
}
String formattedPrice;
if (price == null) {
formattedPrice = nf.format(BigDecimal.ZERO);
} else {
if (price.scale() > MINIMUM_FRACTION_DIGIT) {
nf.setMaximumFractionDigits(price.scale());
}
formattedPrice = nf.format(price);
}
return formattedPrice;
}
示例11: formatDecimal2String
import java.text.DecimalFormat; //导入方法依赖的package包/类
/**
* 格式化数字 double2string
*/
private CharSequence formatDecimal2String(double decimal) {
String decimalScaleStr = "";
String s = mDecimalFill ? "0" : "#";
for (int i = 0; i < mDecimalScale; i++) {
decimalScaleStr += s;
}
DecimalFormat formatter = (DecimalFormat) DecimalFormat.getInstance(Locale.getDefault());
if (mShowCommas && !mShowSymbol) { // 显示数字分号 && 不显示数字符号
formatter.applyPattern(",##0." + decimalScaleStr);
} else if (mShowCommas) { // 显示数字分号 && 显示数字符号
formatter.applyPattern(mSymbol + ",##0." + decimalScaleStr);
} else if (mShowSymbol) { // 不显示数字分号 && 显示数字符号
formatter.applyPattern(mSymbol + "#0." + decimalScaleStr);
} else { // 不显示数字分号 && 不显示数字符号
formatter.applyPattern("#0." + decimalScaleStr);
}
SpannableStringBuilder result = new SpannableStringBuilder(formatter.format(decimal));
if (mShowSymbol) {
if (mSymbolSize == 0) mSymbolSize = getTextSize();
result.setSpan(new AbsoluteSizeSpan((int) mSymbolSize), 0, mSymbol.length(), Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
}
return result;
}
示例12: toString
import java.text.DecimalFormat; //导入方法依赖的package包/类
/**
* Convert to a string with 2 decimal places
*/
public String toString(Locale locale) {
// Decimal formats are generally not synchronized
DecimalFormat formatter = (DecimalFormat) NumberFormat.getNumberInstance(locale);
formatter.applyPattern("#0.##");
return formatter.format(toCurrency());
}
示例13: chronoStringElapsedTime
import java.text.DecimalFormat; //导入方法依赖的package包/类
/**
* Retourne sous forme d'une chaîne de caractères String le temps écoulé depuis la mise
* à zéro du chronomètre interne.
*
* @return le nombre de secondes écoulés jusqu'au 1/1000s
*/
public static String chronoStringElapsedTime() {
Locale locale = Locale.getDefault();
DecimalFormat df = (DecimalFormat) DecimalFormat.getInstance(locale);
df.applyPattern("#,##0.000");
return df.format(chronoElapsedTime());
}
示例14: formatDecimal2String
import java.text.DecimalFormat; //导入方法依赖的package包/类
/**
* 格式化数字 double2string
*/
private CharSequence formatDecimal2String(double decimal) {
String decimalScaleStr = "";
String s = mDecimalFill ? "0" : "#";
for (int i = 0; i < mDecimalScale; i++) {
decimalScaleStr += s;
}
DecimalFormat formatter = (DecimalFormat) DecimalFormat.getInstance(Locale.getDefault());
if (mShowCommas && !mShowSymbol) { // 显示数字分号 && 不显示数字符号
formatter.applyPattern(",##0." + decimalScaleStr);
} else if (mShowCommas) { // 显示数字分号 && 显示数字符号
formatter.applyPattern(mSymbol + ",##0." + decimalScaleStr);
} else if (mShowSymbol) { // 不显示数字分号 && 显示数字符号
formatter.applyPattern(mSymbol + "#0." + decimalScaleStr);
} else { // 不显示数字分号 && 不显示数字符号
formatter.applyPattern("#0." + decimalScaleStr);
}
formatter.setRoundingMode(RoundingMode.DOWN);
SpannableStringBuilder result = new SpannableStringBuilder(formatter.format(decimal));
if (mShowSymbol) {
if (mSymbolSize == 0) mSymbolSize = getTextSize();
result.setSpan(new AbsoluteSizeSpan((int) mSymbolSize), 0, mSymbol.length(), Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
}
return result;
}
示例15: onCreateView
import java.text.DecimalFormat; //导入方法依赖的package包/类
@Override
public View onCreateView(
LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
super.onCreateView(inflater, container, savedInstanceState);
setHasOptionsMenu(true); // this fragment has no menu items to display
// get Bundle of arguments then extract the dsObject
Bundle arguments = getArguments();
if (arguments != null)
dsObject = arguments.getParcelable("dsObjectArrayListItem");
// inflate DetailFragment's layout
View view =
inflater.inflate(R.layout.fragment_details, container, false);
// get the EditTexts
TextView objectIdTextView = view.findViewById(R.id.objectIdTextView);
TextView typeTextView = view.findViewById(R.id.typeTextView);
TextView magTextView = view.findViewById(R.id.magTextView);
TextView sizeTextView = view.findViewById(R.id.sizeTextView);
TextView distTextView = view.findViewById(R.id.distTextView);
TextView raTextView = view.findViewById(R.id.raTextView);
TextView decTextView = view.findViewById(R.id.decTextView);
TextView constTextView = view.findViewById(R.id.constTextView);
TextView nameTextView = view.findViewById(R.id.nameTextView);
psaTextView = view.findViewById(R.id.psaTextView);
oithTextView = view.findViewById(R.id.oithTextView);
skyAtlasTextView = view.findViewById(R.id.skyAtlasTextView);
psaTextViewLabel = view.findViewById(R.id.psaLabelTextView);
oithTextViewLabel = view.findViewById(R.id.oithLabelTextView);
skyAtlasTextViewLabel = view.findViewById(R.id.skyAtlasLabelTextView);
TextView catTextView = view.findViewById(R.id.catTextView);
TextView altTextView = view.findViewById(R.id.altTextView);
TextView azTextView = view.findViewById(R.id.azTextView);
TextView riseTextView = view.findViewById(R.id.riseTextView);
TextView setTextView = view.findViewById(R.id.setTextView);
// set the TextViews
NumberFormat numberFormat = NumberFormat.getInstance(Locale.US);
DecimalFormat df = (DecimalFormat) numberFormat;
df.applyPattern("0.0");
setUserPreferences();
objectIdTextView.setText(dsObject.getDsoObjectID());
String typeAbbr = dsObject.getDsoType();
typeTextView.setText(AstroCalc.getDSOType(typeAbbr));
String magnitude = df.format(dsObject.getDsoMag());
magTextView.setText(magnitude);
sizeTextView.setText(dsObject.getDsoSize());
distTextView.setText(dsObject.getDsoDist());
raTextView.setText(AstroCalc.convertDDToHMS(dsObject.getDsoRA()));
decTextView.setText(AstroCalc.convertDDToDMS(dsObject.getDsoDec()));
String constAbbr = dsObject.getDsoConst();
constTextView.setText(AstroCalc.getConstName(constAbbr));
nameTextView.setText(dsObject.getDsoName());
psaTextView.setText(dsObject.getDsoPSA());
oithTextView.setText(dsObject.getDsoOITH());
skyAtlasTextView.setText(dsObject.getDsoSkyAtlas());
catTextView.setText(dsObject.getDsoCatalogue());
String altitude = df.format(dsObject.getDsoAlt()) + "°";
altTextView.setText(altitude);
String azimuth = df.format(dsObject.getDsoAz()) + "°";
azTextView.setText(azimuth);
riseTextView.setText(dsObject.getDsoRiseTimeStr());
setTextView.setText(dsObject.getDsoSetTimeStr());
// display constellation image
if (!constAbbr.equals("")) {
String constName = "images/" + dsObject.getDsoConst() + ".gif";
PhotoView constImageView = view.findViewById(R.id.constImageView);
Bitmap bm = loadConstImage(constName); // display constellation .gif on detail screen
constImageView.setImageBitmap(bm);
}
return view;
}