Java Code Examples for java.text.DecimalFormat#setMaximumIntegerDigits()

The following examples show how to use java.text.DecimalFormat#setMaximumIntegerDigits() . These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
/**
 * 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);
}
 
Example 2
Source Project: zap-android   File: MonetaryUtil.java    License: MIT License 6 votes vote down vote up
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;
    }
}
 
Example 3
Source Project: util4j   File: TestDecimalFormat.java    License: Apache License 2.0 6 votes vote down vote up
public static void test1(DecimalFormat df) {
	//默认显示3位小数
	double d = 1.5555555;
	System.out.println(df.format(d));//1.556
	//设置小数点后最大位数为5
	df.setMaximumFractionDigits(5);
	df.setMinimumIntegerDigits(15);
	System.out.println(df.format(d));//1.55556
	df.setMaximumFractionDigits(2);
	System.out.println(df.format(d));//1.56
	//设置小数点后最小位数,不够的时候补0
	df.setMinimumFractionDigits(10);
	System.out.println(df.format(d));//1.5555555500
	//设置整数部分最小长度为3,不够的时候补0
	df.setMinimumIntegerDigits(3);
	System.out.println(df.format(d));
	//设置整数部分的最大值为2,当超过的时候会从个位数开始取相应的位数
	df.setMaximumIntegerDigits(2);
	System.out.println(df.format(d));
}
 
Example 4
/**
 * 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);
}
 
Example 5
/** print the idx positions of this alignment
 *
 * @return a String representation
 */
@Override
public String toString(){
	DecimalFormat d2 = new DecimalFormat();
	// the result can be localized. To change this and enforce UK local do...
	//(DecimalFormat)NumberFormat.getInstance(java.util.Locale.UK);
	d2.setMaximumIntegerDigits(3);
	d2.setMinimumFractionDigits(2);
	d2.setMaximumFractionDigits(2);
	StringBuffer s = new StringBuffer();
	s.append("#" + getAltAligNumber() +
			" cluster:" + cluster +
			" eqr:" + getEqr() +
			" rmsd:" + d2.format(getRmsd()) +
			" %id:" + getPercId() +
			" gaps:" + getGaps() +
			" score:" + d2.format(score)	);

	return s.toString();
}
 
Example 6
Source Project: io   File: DcJsonWriter.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * Double型の値を既定のフォーマットで整形する.
 * 情報落ちの起こらない範囲で固定小数点数表現に変換する
 * @param value 整形する値
 * @return 整形結果
 */
private String formatDoubleValue(double value) {
    // 固定小数表現に変換した文字列を生成する
    DecimalFormat format = new DecimalFormat("#.#");
    format.setMaximumIntegerDigits(MAX_INTEGER_DIGITS);
    format.setMaximumFractionDigits(MAX_FRACTION_DIGITS);
    String fomattedValue = format.format(value);

    // 固定小数表現に変換した文字列を一度Double型に変換して
    // 情報落ちがある場合は元の値を返却する
    String result = fomattedValue;
    if (value != Double.parseDouble(fomattedValue)) {
        result = Double.toString(value);
    }
    return result;
}
 
Example 7
/**
 * 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);
}
 
Example 8
/**
 * 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);
}
 
Example 9
Source Project: j2objc   File: DecimalFormatTest.java    License: Apache License 2.0 6 votes vote down vote up
public void testMinimumIntegerDigits_getAndSet() {
    final int minIntDigit = 1;
    DecimalFormat form = (DecimalFormat) NumberFormat.getInstance(Locale.US);

    // getMaximumIntegerDigits from DecimalFormat (default to 1)
    assertEquals(minIntDigit, form.getMinimumIntegerDigits());

    form.setMinimumIntegerDigits(300);
    assertEquals(300, form.getMinimumIntegerDigits());

    // Deliberately > 309. The API docs mention 309 and suggest that you can set the value
    // higher but it will use 309 as a ceiling.
    form.setMinimumIntegerDigits(500);
    assertEquals(500, form.getMinimumIntegerDigits());

    form.setMaximumIntegerDigits(400);
    assertEquals(400, form.getMinimumIntegerDigits());

    form.setMinimumIntegerDigits(-3);
    assertEquals(0, form.getMinimumIntegerDigits());
}
 
Example 10
/**
 * 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);
}
 
Example 11
Source Project: j2objc   File: DecimalFormatTest.java    License: Apache License 2.0 5 votes vote down vote up
public void test_formatLong_minimumIntegerDigits() {
    DecimalFormat df = new DecimalFormat("###0.##", new DecimalFormatSymbols(Locale.US));
    df.setMinimumIntegerDigits(3);
    assertEquals(3, df.getMinimumIntegerDigits());
    assertEquals("012", df.format(12));
    df.setMaximumIntegerDigits(2);
    assertEquals(2, df.getMinimumIntegerDigits());
    assertEquals("00.7", df.format(0.7));
}
 
Example 12
Source Project: j2objc   File: DecimalFormatTest.java    License: Apache License 2.0 5 votes vote down vote up
public void test_formatDouble_maxFractionDigits() {
    final DecimalFormatSymbols dfs = new DecimalFormatSymbols(Locale.US);
    DecimalFormat format = new DecimalFormat("#0.#", dfs);
    format.setGroupingUsed(false);
    format.setMaximumIntegerDigits(400);
    format.setMaximumFractionDigits(1);

    assertEquals("1", format.format(0.99));
    assertEquals("1", format.format(0.95));
    assertEquals("0.9", format.format(0.94));
    assertEquals("0.9", format.format(0.90));

    assertEquals("0.2", format.format(0.19));
    assertEquals("0.2", format.format(0.15));
    assertEquals("0.1", format.format(0.14));
    assertEquals("0.1", format.format(0.10));

    format.setMaximumFractionDigits(10);
    assertEquals("1", format.format(0.99999999999));
    assertEquals("1", format.format(0.99999999995));
    assertEquals("0.9999999999", format.format(0.99999999994));
    assertEquals("0.9999999999", format.format(0.99999999990));

    assertEquals("0.1111111112", format.format(0.11111111119));
    assertEquals("0.1111111112", format.format(0.11111111115));
    assertEquals("0.1111111111", format.format(0.11111111114));
    assertEquals("0.1111111111", format.format(0.11111111110));

    format.setMaximumFractionDigits(14);
    assertEquals("1", format.format(0.999999999999999));
    assertEquals("1", format.format(0.999999999999995));
    assertEquals("0.99999999999999", format.format(0.999999999999994));
    assertEquals("0.99999999999999", format.format(0.999999999999990));

    assertEquals("0.11111111111112", format.format(0.111111111111119));
    assertEquals("0.11111111111112", format.format(0.111111111111115));
    assertEquals("0.11111111111111", format.format(0.111111111111114));
    assertEquals("0.11111111111111", format.format(0.111111111111110));
}
 
Example 13
Source Project: zap-android   File: MonetaryUtil.java    License: MIT License 5 votes vote down vote up
private String formatAsSatoshiDisplayAmount(long value) {
    Locale loc = mContext.getResources().getConfiguration().locale;
    NumberFormat nf = NumberFormat.getNumberInstance(loc);
    DecimalFormat df = (DecimalFormat) nf;
    df.setMinimumIntegerDigits(1);
    df.setMaximumIntegerDigits(16);
    return df.format(value);
}
 
Example 14
Source Project: zap-android   File: MonetaryUtil.java    License: MIT License 5 votes vote down vote up
private String formatAsFiatDisplayAmount(double value) {
    Locale loc = mContext.getResources().getConfiguration().locale;
    NumberFormat nf = NumberFormat.getNumberInstance(loc);
    DecimalFormat df = (DecimalFormat) nf;
    df.setMaximumFractionDigits(2);
    df.setMinimumFractionDigits(2);
    df.setMinimumIntegerDigits(1);
    df.setMaximumIntegerDigits(22);
    String result = df.format(value);
    return result;
}
 
Example 15
Source Project: j2objc   File: DecimalFormatTest.java    License: Apache License 2.0 5 votes vote down vote up
public void test_formatDouble_roundingTo15Digits() throws Exception {
    final DecimalFormatSymbols dfs = new DecimalFormatSymbols(Locale.US);
    DecimalFormat df = new DecimalFormat("#.#", dfs);
    df.setMaximumIntegerDigits(400);
    df.setGroupingUsed(false);

    df.setMaximumFractionDigits(0);
    assertEquals("1000000000000000", df.format(999999999999999.9));
    df.setMaximumFractionDigits(1);
    assertEquals("100000000000000", df.format(99999999999999.99));
    df.setMaximumFractionDigits(2);
    assertEquals("10000000000000", df.format(9999999999999.999));
    df.setMaximumFractionDigits(3);
    assertEquals("1000000000000", df.format(999999999999.9999));
    df.setMaximumFractionDigits(4);
    assertEquals("100000000000", df.format(99999999999.99999));
    df.setMaximumFractionDigits(5);
    assertEquals("10000000000", df.format(9999999999.999999));
    df.setMaximumFractionDigits(6);
    assertEquals("1000000000", df.format(999999999.9999999));
    df.setMaximumFractionDigits(7);
    assertEquals("100000000", df.format(99999999.99999999));
    df.setMaximumFractionDigits(8);
    assertEquals("10000000", df.format(9999999.999999999));
    df.setMaximumFractionDigits(9);
    assertEquals("1000000", df.format(999999.9999999999));
    df.setMaximumFractionDigits(10);
    assertEquals("100000", df.format(99999.99999999999));
    df.setMaximumFractionDigits(11);
    assertEquals("10000", df.format(9999.999999999999));
    df.setMaximumFractionDigits(12);
    assertEquals("1000", df.format(999.9999999999999));
    df.setMaximumFractionDigits(13);
    assertEquals("100", df.format(99.99999999999999));
    df.setMaximumFractionDigits(14);
    assertEquals("10", df.format(9.999999999999999));
    df.setMaximumFractionDigits(15);
    assertEquals("1", df.format(0.9999999999999999));
}
 
Example 16
Source Project: development   File: PriceConverter.java    License: Apache License 2.0 5 votes vote down vote up
@Override
protected Format getFormat(String pattern, Locale locale) {
    DecimalFormat format = (DecimalFormat) super.getFormat(pattern,
            locale);
    format.setMaximumIntegerDigits(NUMBER_OF_INTEGER_PLACES);
    format.setMaximumFractionDigits(NUMBER_OF_DECIMAL_PLACES);
    // avoid lost precision due to parsing to double:
    format.setParseBigDecimal(true);
    return format;
}
 
Example 17
Source Project: j2objc   File: DecimalFormatTest.java    License: Apache License 2.0 5 votes vote down vote up
private static void assertDecimalFormatIsLossless(double d) throws Exception {
    final DecimalFormatSymbols dfs = new DecimalFormatSymbols(Locale.US);
    DecimalFormat format = new DecimalFormat("#0.#", dfs);
    format.setGroupingUsed(false);
    format.setMaximumIntegerDigits(400);
    format.setMaximumFractionDigits(400);

    // Every floating point binary can be represented exactly in decimal if you have enough
    // digits. This shows the value actually being tested.
    String testId = "decimalValue: " + new BigDecimal(d);

    // As a sanity check we try out parseDouble() with the string generated by
    // Double.toString(). Strictly speaking Double.toString() is probably not guaranteed to be
    // lossless, but in reality it probably is, or at least is close enough.
    assertDoubleEqual(
            testId + " failed parseDouble(toString()) sanity check",
            d, Double.parseDouble(Double.toString(d)));

    // Format the number: If this is lossy it is a problem. We are trying to check that it
    // doesn't lose any unnecessary precision.
    String result = format.format(d);

    // Here we use Double.parseDouble() which should able to parse a number we know was
    // representable as a double into the original double. If parseDouble() is not implemented
    // correctly the test is invalid.
    double doubleParsed = Double.parseDouble(result);
    assertDoubleEqual(testId + " (format() produced " + result + ")",
            d, doubleParsed);

    // For completeness we try to parse using the formatter too. If this fails but the format
    // above didn't it may be a problem with parse(), or with format() that we didn't spot.
    assertDoubleEqual(testId + " failed parse(format()) check",
            d, format.parse(result).doubleValue());
}
 
Example 18
Source Project: j2objc   File: DecimalFormatTest.java    License: Apache License 2.0 5 votes vote down vote up
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());
}
 
Example 19
Source Project: j2objc   File: DecimalFormatTest.java    License: Apache License 2.0 5 votes vote down vote up
public void test_formatLong_maximumIntegerDigits() {
    DecimalFormat df = new DecimalFormat("###0.##");
    df.setMaximumIntegerDigits(2);
    assertEquals(2, df.getMaximumIntegerDigits());
    assertEquals("34", df.format(1234));
    df.setMinimumIntegerDigits(4);
    assertEquals(4, df.getMaximumIntegerDigits());
    assertEquals("0026", df.format(26));
}
 
Example 20
Source Project: xDrip   File: Simulation.java    License: GNU General Public License v3.0 4 votes vote down vote up
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
}