下面列出了java.text.DecimalFormat#setMinimumIntegerDigits ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* Test that we can configure the RelativeDateFormat to show
* hh:mm:ss.
*/
public void test2033092() {
RelativeDateFormat rdf = new RelativeDateFormat();
rdf.setShowZeroDays(false);
rdf.setShowZeroHours(false);
rdf.setMinuteSuffix(":");
rdf.setHourSuffix(":");
rdf.setSecondSuffix("");
DecimalFormat hoursFormatter = new DecimalFormat();
hoursFormatter.setMaximumFractionDigits(0);
hoursFormatter.setMaximumIntegerDigits(2);
hoursFormatter.setMinimumIntegerDigits(2);
rdf.setHourFormatter(hoursFormatter);
DecimalFormat minsFormatter = new DecimalFormat();
minsFormatter.setMaximumFractionDigits(0);
minsFormatter.setMaximumIntegerDigits(2);
minsFormatter.setMinimumIntegerDigits(2);
rdf.setMinuteFormatter(minsFormatter);
DecimalFormat secondsFormatter = new DecimalFormat();
secondsFormatter.setMaximumFractionDigits(0);
secondsFormatter.setMaximumIntegerDigits(2);
secondsFormatter.setMinimumIntegerDigits(2);
rdf.setSecondFormatter(secondsFormatter);
String s = rdf.format(new Date(2 * 60L * 60L * 1000L + 122500L));
assertEquals("02:02:02", s);
}
private String formatAsBitsDisplayAmount(long value) {
Locale loc = mContext.getResources().getConfiguration().locale;
NumberFormat nf = NumberFormat.getNumberInstance(loc);
DecimalFormat df = (DecimalFormat) nf;
df.setMaximumFractionDigits(2);
df.setMinimumIntegerDigits(1);
df.setMaximumIntegerDigits(22);
String result = df.format(value / 100d);
// If we have a fraction, then always show 2 fraction digits for bits
if (result.contains(String.valueOf(df.getDecimalFormatSymbols().getDecimalSeparator()))) {
df.setMinimumFractionDigits(2);
return df.format(value / 100d);
} else {
return result;
}
}
/**
* Test that we can configure the RelativeDateFormat to show
* hh:mm:ss.
*/
public void test2033092() {
RelativeDateFormat rdf = new RelativeDateFormat();
rdf.setShowZeroDays(false);
rdf.setShowZeroHours(false);
rdf.setMinuteSuffix(":");
rdf.setHourSuffix(":");
rdf.setSecondSuffix("");
DecimalFormat hoursFormatter = new DecimalFormat();
hoursFormatter.setMaximumFractionDigits(0);
hoursFormatter.setMaximumIntegerDigits(2);
hoursFormatter.setMinimumIntegerDigits(2);
rdf.setHourFormatter(hoursFormatter);
DecimalFormat minsFormatter = new DecimalFormat();
minsFormatter.setMaximumFractionDigits(0);
minsFormatter.setMaximumIntegerDigits(2);
minsFormatter.setMinimumIntegerDigits(2);
rdf.setMinuteFormatter(minsFormatter);
DecimalFormat secondsFormatter = new DecimalFormat();
secondsFormatter.setMaximumFractionDigits(0);
secondsFormatter.setMaximumIntegerDigits(2);
secondsFormatter.setMinimumIntegerDigits(2);
rdf.setSecondFormatter(secondsFormatter);
String s = rdf.format(new Date(2 * 60L * 60L * 1000L + 122500L));
assertEquals("02:02:02", s);
}
/**
* Test that we can configure the RelativeDateFormat to show
* hh:mm:ss.
*/
public void test2033092() {
RelativeDateFormat rdf = new RelativeDateFormat();
rdf.setShowZeroDays(false);
rdf.setShowZeroHours(false);
rdf.setMinuteSuffix(":");
rdf.setHourSuffix(":");
rdf.setSecondSuffix("");
DecimalFormat hoursFormatter = new DecimalFormat();
hoursFormatter.setMaximumFractionDigits(0);
hoursFormatter.setMaximumIntegerDigits(2);
hoursFormatter.setMinimumIntegerDigits(2);
rdf.setHourFormatter(hoursFormatter);
DecimalFormat minsFormatter = new DecimalFormat();
minsFormatter.setMaximumFractionDigits(0);
minsFormatter.setMaximumIntegerDigits(2);
minsFormatter.setMinimumIntegerDigits(2);
rdf.setMinuteFormatter(minsFormatter);
DecimalFormat secondsFormatter = new DecimalFormat();
secondsFormatter.setMaximumFractionDigits(0);
secondsFormatter.setMaximumIntegerDigits(2);
secondsFormatter.setMinimumIntegerDigits(2);
rdf.setSecondFormatter(secondsFormatter);
String s = rdf.format(new Date(2 * 60L * 60L * 1000L + 122500L));
assertEquals("02:02:02", s);
}
/**
* Test that we can configure the RelativeDateFormat to show
* hh:mm:ss.
*/
public void test2033092() {
RelativeDateFormat rdf = new RelativeDateFormat();
rdf.setShowZeroDays(false);
rdf.setShowZeroHours(false);
rdf.setMinuteSuffix(":");
rdf.setHourSuffix(":");
rdf.setSecondSuffix("");
DecimalFormat hoursFormatter = new DecimalFormat();
hoursFormatter.setMaximumFractionDigits(0);
hoursFormatter.setMaximumIntegerDigits(2);
hoursFormatter.setMinimumIntegerDigits(2);
rdf.setHourFormatter(hoursFormatter);
DecimalFormat minsFormatter = new DecimalFormat();
minsFormatter.setMaximumFractionDigits(0);
minsFormatter.setMaximumIntegerDigits(2);
minsFormatter.setMinimumIntegerDigits(2);
rdf.setMinuteFormatter(minsFormatter);
DecimalFormat secondsFormatter = new DecimalFormat();
secondsFormatter.setMaximumFractionDigits(0);
secondsFormatter.setMaximumIntegerDigits(2);
secondsFormatter.setMinimumIntegerDigits(2);
rdf.setSecondFormatter(secondsFormatter);
String s = rdf.format(new Date(2 * 60L * 60L * 1000L + 122500L));
assertEquals("02:02:02", s);
}
public static String qs(double x, int digits) {
if (digits == -1) {
digits = 0;
if (((int) x != x)) {
digits++;
if ((((int) x * 10) / 10 != x)) {
digits++;
if ((((int) x * 100) / 100 != x)) digits++;
}
}
}
DecimalFormatSymbols symbols = new DecimalFormatSymbols();
symbols.setDecimalSeparator('.');
DecimalFormat df = new DecimalFormat("#", symbols);
df.setMaximumFractionDigits(digits);
df.setMinimumIntegerDigits(1);
return df.format(x);
}
/**
* @param value a value in the graph
* @return the string representation of the value (using quotes if it is already a string)
*/
public static String valueToString(Object value) {
if (value instanceof String) {
return quoteString((String) value);
} else if (value instanceof Double) {
DecimalFormat df = new DecimalFormat("#", DecimalFormatSymbols.getInstance(Locale.ENGLISH));
df.setMinimumFractionDigits(1);
df.setMaximumFractionDigits(12);
df.setMinimumIntegerDigits(1);
return df.format(value);
} else {
return value.toString();
}
}
/**
* Print the matrix to the output stream. Line the elements up in columns with a Fortran-like 'Fw.d' style format.
*
* @param output Output stream.
* @param w Column width.
* @param d Number of digits after the decimal.
*/
public void print(final PrintWriter output, final int w, final int d) {
final DecimalFormat format = new DecimalFormat();
format.setDecimalFormatSymbols(new DecimalFormatSymbols(Locale.US));
format.setMinimumIntegerDigits(1);
format.setMaximumFractionDigits(d);
format.setMinimumFractionDigits(d);
format.setGroupingUsed(false);
print(output, format, w + 2);
}
/** Print the matrix to the output stream. Line the elements up in
* columns with a Fortran-like 'Fw.d' style format.
@param output Output stream.
@param w Column width.
@param d Number of digits after the decimal.
*/
public void print (PrintWriter output, int w, int d) {
DecimalFormat format = new DecimalFormat();
format.setDecimalFormatSymbols(new DecimalFormatSymbols(Locale.US));
format.setMinimumIntegerDigits(1);
format.setMaximumFractionDigits(d);
format.setMinimumFractionDigits(d);
format.setGroupingUsed(false);
print(output,format,w+2);
}
/**
* @param lines The number of lines in the longest file.
* Used to find out how many digits a line number should be.
* Ex: if lines is 12, line one should be shown as 01, but
* if lines is 100, line one should be shown as 001.
*/
public RhnHtmlDiffWriter(int lines) {
onlyChanged = false;
oldfile = new StringBuffer();
newfile = new StringBuffer();
formatter = new DecimalFormat();
formatter.setMaximumFractionDigits(0);
formatter.setMinimumIntegerDigits(Integer.toString(lines).length());
}
public static String unitizedDeltaStringRaw(boolean showUnit, boolean highGranularity,double value, boolean doMgdl) {
if (Math.abs(value) > 100) {
// a delta > 100 will not happen with real BG values -> problematic sensor data
return "ERR";
}
// TODO: allow localization from os settings once pebble doesn't require english locale
DecimalFormat df = new DecimalFormat("#", new DecimalFormatSymbols(Locale.ENGLISH));
String delta_sign = "";
if (value > 0) {
delta_sign = "+";
}
if (doMgdl) {
if (highGranularity) {
df.setMaximumFractionDigits(1);
} else {
df.setMaximumFractionDigits(0);
}
return delta_sign + df.format(unitized(value,doMgdl)) + (showUnit ? " mg/dl" : "");
} else {
// only show 2 decimal places on mmol/l delta when less than 0.1 mmol/l
if (highGranularity && (Math.abs(value) < (Constants.MMOLL_TO_MGDL * 0.1))) {
df.setMaximumFractionDigits(2);
} else {
df.setMaximumFractionDigits(1);
}
df.setMinimumFractionDigits(1);
df.setMinimumIntegerDigits(1);
return delta_sign + df.format(unitized(value,doMgdl)) + (showUnit ? " mmol/l" : "");
}
}
public void testMaximumFactionDigits_maxChangesMin() {
DecimalFormat form = (DecimalFormat) NumberFormat.getInstance(Locale.US);
form.setMinimumFractionDigits(200);
form.setMaximumFractionDigits(100);
assertEquals(100, form.getMaximumFractionDigits());
assertEquals(100, form.getMinimumFractionDigits());
form.setMinimumIntegerDigits(200);
form.setMaximumIntegerDigits(100);
assertEquals(100, form.getMaximumIntegerDigits());
assertEquals(100, form.getMinimumIntegerDigits());
}
/**
* Hybrid toString.
*
* @see print(int w, int d)
*/
public String toString(int w, int d) {
DecimalFormat format = new DecimalFormat();
format.setDecimalFormatSymbols(new DecimalFormatSymbols(Locale.US));
format.setMinimumIntegerDigits(1);
format.setMaximumFractionDigits(d);
format.setMinimumFractionDigits(d);
format.setGroupingUsed(false);
w += 2;
String s;
String result = "";
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
s = format.format(A[i][j]); // format the number
int padding =
Math.max(1, w - result.length()); // At _least_ 1 space
result += s;
for (int k = 0; k < padding; k++)
result += " ";
}
result += "\n";
}
return result;
}
/** Print the matrix to the output stream. Line the elements up in
* columns with a Fortran-like 'Fw.d' style format.
@param output Output stream.
@param w Column width.
@param d Number of digits after the decimal.
*/
public void print (PrintWriter output, int w, int d) {
DecimalFormat format = new DecimalFormat();
format.setDecimalFormatSymbols(new DecimalFormatSymbols(Locale.US));
format.setMinimumIntegerDigits(1);
format.setMaximumFractionDigits(d);
format.setMinimumFractionDigits(d);
format.setGroupingUsed(false);
print(output,format,w+2);
}
/**
* Source: http://www.luschny.de/java/doubleformat.html
*
* @param dValue
* the value to be formatted
* @param nScale
* The precision of the decimal scale. If type is
* {@link EDecimalType#FIX} the decimal scale, else the (carrying scale
* - 1). Should be ≥ 0.
* @param eType
* The formatting type. May not be <code>null</code>.
* @param aLocale
* The locale to be used for the decimal symbols. May not be
* <code>null</code>.
* @return the string representation of the double value. For NaN and infinite
* values, the return of {@link Double#toString()} is returned.
*/
@Nonnull
public static String getFormatted (final double dValue,
@Nonnegative final int nScale,
@Nonnull final EDecimalType eType,
@Nonnull final Locale aLocale)
{
ValueEnforcer.isGE0 (nScale, "Scale");
ValueEnforcer.notNull (eType, "Type");
ValueEnforcer.notNull (aLocale, "Locale");
if (Double.isNaN (dValue) || Double.isInfinite (dValue))
return Double.toString (dValue);
// Avoid negative scales
final DecimalFormat aDF = (DecimalFormat) NumberFormat.getInstance (aLocale);
aDF.setDecimalFormatSymbols (DecimalFormatSymbols.getInstance (aLocale));
aDF.setMaximumFractionDigits (nScale);
aDF.setMinimumFractionDigits (nScale);
if (eType.isExponential ())
{
String sPattern = "0E0";
if (nScale > 0)
sPattern += '.' + StringHelper.getRepeated ('0', nScale);
aDF.applyPattern (sPattern);
}
else
{
aDF.setGroupingUsed (false);
aDF.setMinimumIntegerDigits (1);
}
return aDF.format (dValue);
}
public String unitizedDeltaString(boolean showUnit, boolean highGranularity) {
List<BgReading> last2 = BgReading.latest(2);
if(last2.size() < 2 || last2.get(0).timestamp - last2.get(1).timestamp > MAX_SLOPE_MINUTES * 60 * 1000){
// don't show delta if there are not enough values or the values are more than 20 mintes apart
return "???";
}
double value = BgReading.currentSlope() * 5*60*1000;
if(Math.abs(value) > 100){
// a delta > 100 will not happen with real BG values -> problematic sensor data
return "ERR";
}
// TODO: allow localization from os settings once pebble doesn't require english locale
DecimalFormat df = new DecimalFormat("#", new DecimalFormatSymbols(Locale.ENGLISH));
String delta_sign = "";
if (value > 0) { delta_sign = "+"; }
if(doMgdl) {
if(highGranularity){
df.setMaximumFractionDigits(1);
} else {
df.setMaximumFractionDigits(0);
}
return delta_sign + df.format(unitized(value)) + (showUnit?" mg/dl":"");
} else {
if(highGranularity){
df.setMaximumFractionDigits(2);
} else {
df.setMaximumFractionDigits(1);
}
df.setMinimumFractionDigits(1);
df.setMinimumIntegerDigits(1);
return delta_sign + df.format(unitized(value)) + (showUnit?" mmol/l":"");
}
}
private void handleWordPair() {
if ((thisnumber == -1) || "".equals(thisword)) {
return;
}
Log.d(TAG, "GOT WORD PAIR: " + thisnumber + " = " + thisword);
switch (thisword) {
case "watchkeypad":
if ((!watchkeypadset) && (thisnumber > 0)) {
watchkeypad = true;
watchkeypadset = true;
Log.d(TAG, "Treatment entered on watchkeypad: " + Double.toString(thisnumber));
} else {
Log.d(TAG, "watchkeypad already set");
}
break;
case "rapid":
case "units":
if ((!insulinset) && (thisnumber > 0)) {
thisinsulinnumber = thisnumber;
Log.d(TAG, "Rapid dose: " + Double.toString(thisnumber));
insulinset = true;
} else {
Log.d(TAG, "Rapid dose already set");
}
break;
case "carbs":
if ((!carbsset) && (thisnumber > 0)) {
thiscarbsnumber = thisnumber;
carbsset = true;
Log.d(TAG, "Carbs eaten: " + Double.toString(thisnumber));
} else {
Log.d(TAG, "Carbs already set");
}
break;
case "blood":
if ((!glucoseset) && (thisnumber > 0)) {
thisglucosenumber = thisnumber;
Log.d(TAG, "Blood test: " + Double.toString(thisnumber));
glucoseset = true;
} else {
Log.d(TAG, "Blood glucose already set");
}
break;
case "time":
Log.d(TAG, "processing time keyword");
if ((!timeset) && (thisnumber >= 0)) {
final NumberFormat nf = NumberFormat.getNumberInstance(Locale.US);
final DecimalFormat df = (DecimalFormat) nf;
//DecimalFormat df = new DecimalFormat("#");
df.setMinimumIntegerDigits(2);
df.setMinimumFractionDigits(2);
df.setMaximumFractionDigits(2);
df.setMaximumIntegerDigits(2);
final Calendar c = Calendar.getInstance();
final SimpleDateFormat simpleDateFormat1 =
new SimpleDateFormat("dd/M/yyyy ", Locale.US);
final SimpleDateFormat simpleDateFormat2 =
new SimpleDateFormat("dd/M/yyyy HH.mm", Locale.US); // TODO double check 24 hour 12.00 etc
final String datenew = simpleDateFormat1.format(c.getTime()) + df.format(thisnumber);
Log.d(TAG, "Time Timing data datenew: " + datenew);
final Date datethen;
final Date datenow = new Date();
try {
datethen = simpleDateFormat2.parse(datenew);
double difference = datenow.getTime() - datethen.getTime();
// is it more than 1 hour in the future? If so it must be yesterday
if (difference < -(1000 * 60 * 60)) {
difference = difference + (86400 * 1000);
} else {
// - midnight feast pre-bolus nom nom
if (difference > (60 * 60 * 23 * 1000))
difference = difference - (86400 * 1000);
}
Log.d(TAG, "Time Timing data: " + df.format(thisnumber) + " = difference ms: " + JoH.qs(difference));
thistimetext = df.format(thisnumber);
timeset = true;
thistimeoffset = difference;
} catch (ParseException e) {
// toast to explain?
Log.d(TAG, "Got exception parsing date time");
}
} else {
Log.d(TAG, "Time data already set");
}
break;
} // end switch
}
private void handleWordPair() {
if ((thisnumber == -1) || "".equals(thisword)) {
return;
}
Log.d(TAG, "GOT WORD PAIR: " + thisnumber + " = " + thisword);
switch (thisword) {
case "watchkeypad":
if ((!watchkeypadset) && (thisnumber > 0)) {
watchkeypad = true;
watchkeypadset = true;
Log.d(TAG, "Treatment entered on watchkeypad: " + Double.toString(thisnumber));
} else {
Log.d(TAG, "watchkeypad already set");
}
break;
case "rapid":
case "units":
if ((!insulinset) && (thisnumber > 0)) {
thisinsulinnumber = thisnumber;
Log.d(TAG, "Rapid dose: " + Double.toString(thisnumber));
insulinset = true;
} else {
Log.d(TAG, "Rapid dose already set");
}
break;
case "carbs":
if ((!carbsset) && (thisnumber > 0)) {
thiscarbsnumber = thisnumber;
carbsset = true;
Log.d(TAG, "Carbs eaten: " + Double.toString(thisnumber));
} else {
Log.d(TAG, "Carbs already set");
}
break;
case "blood":
if ((!glucoseset) && (thisnumber > 0)) {
thisglucosenumber = thisnumber;
Log.d(TAG, "Blood test: " + Double.toString(thisnumber));
glucoseset = true;
} else {
Log.d(TAG, "Blood glucose already set");
}
break;
case "time":
Log.d(TAG, "processing time keyword");
if ((!timeset) && (thisnumber >= 0)) {
final NumberFormat nf = NumberFormat.getNumberInstance(Locale.US);
final DecimalFormat df = (DecimalFormat) nf;
//DecimalFormat df = new DecimalFormat("#");
df.setMinimumIntegerDigits(2);
df.setMinimumFractionDigits(2);
df.setMaximumFractionDigits(2);
df.setMaximumIntegerDigits(2);
final Calendar c = Calendar.getInstance();
final SimpleDateFormat simpleDateFormat1 =
new SimpleDateFormat("dd/M/yyyy ", Locale.US);
final SimpleDateFormat simpleDateFormat2 =
new SimpleDateFormat("dd/M/yyyy HH.mm", Locale.US); // TODO double check 24 hour 12.00 etc
final String datenew = simpleDateFormat1.format(c.getTime()) + df.format(thisnumber);
Log.d(TAG, "Time Timing data datenew: " + datenew);
final Date datethen;
final Date datenow = new Date();
try {
datethen = simpleDateFormat2.parse(datenew);
double difference = datenow.getTime() - datethen.getTime();
// is it more than 1 hour in the future? If so it must be yesterday
if (difference < -(1000 * 60 * 60)) {
difference = difference + (86400 * 1000);
} else {
// - midnight feast pre-bolus nom nom
if (difference > (60 * 60 * 23 * 1000))
difference = difference - (86400 * 1000);
}
Log.d(TAG, "Time Timing data: " + df.format(thisnumber) + " = difference ms: " + JoH.qs(difference));
thistimetext = df.format(thisnumber);
timeset = true;
thistimeoffset = difference;
} catch (ParseException e) {
// toast to explain?
Log.d(TAG, "Got exception parsing date time");
}
} else {
Log.d(TAG, "Time data already set");
}
break;
} // end switch
}
/**
* initializes numeric format pattern
*
* @param patternStr
* ths string used for formatting numeric data
*/
public void applyPattern( String patternStr )
{
try
{
patternStr = processPatternAttributes( patternStr );
this.formatPattern = patternStr;
hexFlag = false;
roundPrecision = -1;
realPattern = formatPattern;
// null format String
if ( this.formatPattern == null )
{
numberFormat = NumberFormat.getInstance( locale.toLocale( ) );
numberFormat.setGroupingUsed( false );
DecimalFormatSymbols symbols = new DecimalFormatSymbols( locale
.toLocale( ) );
decimalSeparator = symbols.getDecimalSeparator( );
decimalFormat = new DecimalFormat( "", //$NON-NLS-1$
new DecimalFormatSymbols( locale.toLocale( ) ) );
decimalFormat.setMinimumIntegerDigits( 1 );
decimalFormat.setGroupingUsed( false );
roundPrecision = getRoundPrecision( numberFormat );
applyPatternAttributes( );
return;
}
// Single character format string
if ( patternStr.length( ) == 1 )
{
handleSingleCharFormatString( patternStr.charAt( 0 ) );
roundPrecision = getRoundPrecision( numberFormat );
applyPatternAttributes( );
return;
}
// Named formats and arbitrary format string
handleNamedFormats( patternStr );
roundPrecision = getRoundPrecision( numberFormat );
applyPatternAttributes( );
}
catch ( Exception illeagueE )
{
logger.log( Level.WARNING, illeagueE.getMessage( ), illeagueE );
}
}
public String oldunitizedDeltaString(boolean showUnit, boolean highGranularity) {
List<BgReading> last2 = BgReading.latest(2);
if(last2.size() < 2 || last2.get(0).timestamp - last2.get(1).timestamp > MAX_SLOPE_MINUTES * 60 * 1000){
// don't show delta if there are not enough values or the values are more than 20 mintes apart
return "???";
}
double value = BgReading.currentSlope() * 5*60*1000;
if(Math.abs(value) > 100){
// a delta > 100 will not happen with real BG values -> problematic sensor data
return "ERR";
}
// TODO: allow localization from os settings once pebble doesn't require english locale
DecimalFormat df = new DecimalFormat("#", new DecimalFormatSymbols(Locale.ENGLISH));
String delta_sign = "";
if (value > 0) { delta_sign = "+"; }
if(doMgdl) {
if(highGranularity){
df.setMaximumFractionDigits(1);
} else {
df.setMaximumFractionDigits(0);
}
return delta_sign + df.format(unitized(value)) + (showUnit?" mg/dl":"");
} else {
if(highGranularity){
df.setMaximumFractionDigits(2);
} else {
df.setMaximumFractionDigits(1);
}
df.setMinimumFractionDigits(1);
df.setMinimumIntegerDigits(1);
return delta_sign + df.format(unitized(value)) + (showUnit?" mmol/l":"");
}
}