Search in sources :

Example 16 with DecimalFormatSymbols

use of java.text.DecimalFormatSymbols in project marine-api by ktuukkan.

the class SentenceParser method setDoubleValue.

/**
	 * Set double value in specified field, with given number of digits before
	 * and after the decimal separator ('.'). When necessary, the value is
	 * padded with leading zeros and/or rounded to meet the requested number of
	 * digits.
	 * 
	 * @param index Field index
	 * @param value Value to set
	 * @param leading Number of digits before decimal separator
	 * @param decimals Maximum number of digits after decimal separator
	 * @see #setDoubleValue(int, double)
	 */
protected final void setDoubleValue(int index, double value, int leading, int decimals) {
    StringBuilder pattern = new StringBuilder();
    for (int i = 0; i < leading; i++) {
        pattern.append('0');
    }
    if (decimals > 0) {
        pattern.append('.');
        for (int i = 0; i < decimals; i++) {
            pattern.append('0');
        }
    }
    if (pattern.length() == 0) {
        pattern.append('0');
    }
    DecimalFormat nf = new DecimalFormat(pattern.toString());
    DecimalFormatSymbols dfs = new DecimalFormatSymbols();
    dfs.setDecimalSeparator('.');
    nf.setDecimalFormatSymbols(dfs);
    setStringValue(index, nf.format(value));
}
Also used : DecimalFormatSymbols(java.text.DecimalFormatSymbols) DecimalFormat(java.text.DecimalFormat)

Example 17 with DecimalFormatSymbols

use of java.text.DecimalFormatSymbols in project marine-api by ktuukkan.

the class TTMParser method setTime.

/*
	 * (non-Javadoc)
	 *
	 * @see net.sf.marineapi.nmea.sentence.TimeSentence#setTime()
	 */
@Override
public void setTime(Time t) {
    /*
		 * The TTM specification calls for seconds with TWO decimals, not the
		 * usual three implemented by the Time.toString(). So we create our own
		 * string.
		 */
    String str = String.format("%02d%02d", t.getHour(), t.getMinutes());
    DecimalFormat nf = new DecimalFormat("00.00");
    DecimalFormatSymbols dfs = new DecimalFormatSymbols();
    dfs.setDecimalSeparator('.');
    nf.setDecimalFormatSymbols(dfs);
    str += nf.format(t.getSeconds());
    setStringValue(UTC_TIME, str);
}
Also used : DecimalFormatSymbols(java.text.DecimalFormatSymbols) DecimalFormat(java.text.DecimalFormat)

Example 18 with DecimalFormatSymbols

use of java.text.DecimalFormatSymbols in project j2objc by google.

the class DecimalFormatSymbolsTest method testSerializationOfMultiCharNegativeAndPercentage.

// https://code.google.com/p/android/issues/detail?id=170718
public void testSerializationOfMultiCharNegativeAndPercentage() throws Exception {
    DecimalFormatSymbols dfs = new DecimalFormatSymbols(Locale.forLanguageTag("ar-AR"));
    // TODO(user): Investigate.
    // assertTrue(dfs.getMinusSignString().length() > 1);
    // assertTrue(dfs.getPercentString().length() > 1);
    // Serialize...
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    new ObjectOutputStream(out).writeObject(dfs);
    byte[] bytes = out.toByteArray();
    // Deserialize...
    ObjectInputStream in = new ObjectInputStream(new ByteArrayInputStream(bytes));
    DecimalFormatSymbols deserializedDfs = (DecimalFormatSymbols) in.readObject();
    assertEquals(-1, in.read());
    assertEquals(dfs.getMinusSign(), deserializedDfs.getMinusSign());
    assertEquals(dfs.getPercent(), deserializedDfs.getPercent());
}
Also used : DecimalFormatSymbols(java.text.DecimalFormatSymbols) ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ObjectOutputStream(java.io.ObjectOutputStream) ObjectInputStream(java.io.ObjectInputStream)

Example 19 with DecimalFormatSymbols

use of java.text.DecimalFormatSymbols in project j2objc by google.

the class NumberFormatTest method test_issue79925.

// https://code.google.com/p/android/issues/detail?id=79925\
// When switching currency after having initialised a DecimalFormat instance to a currency,
// the symbols are missing.
public void test_issue79925() {
    NumberFormat nf = NumberFormat.getCurrencyInstance(Locale.US);
    nf.setCurrency(Currency.getInstance("EUR"));
    assertEquals("€50.00", nf.format(50.0));
    DecimalFormatSymbols decimalFormatSymbols = ((DecimalFormat) nf).getDecimalFormatSymbols();
    decimalFormatSymbols.setCurrencySymbol("");
    ((DecimalFormat) nf).setDecimalFormatSymbols(decimalFormatSymbols);
    assertEquals("50.00", nf.format(50.0));
    nf.setCurrency(Currency.getInstance("SGD"));
    assertEquals("SGD50.00", nf.format(50.0));
    nf.setCurrency(Currency.getInstance("SGD"));
    assertEquals("SGD50.00", nf.format(50.00));
    nf.setCurrency(Currency.getInstance("USD"));
    assertEquals("$50.00", nf.format(50.0));
    nf.setCurrency(Currency.getInstance("SGD"));
    assertEquals("SGD50.00", nf.format(50.0));
}
Also used : DecimalFormatSymbols(java.text.DecimalFormatSymbols) DecimalFormat(java.text.DecimalFormat) NumberFormat(java.text.NumberFormat)

Example 20 with DecimalFormatSymbols

use of java.text.DecimalFormatSymbols in project j2objc by google.

the class NumberFormatTest method test_customCurrencySymbol.

// Test to ensure explicitly setting a currency symbol will overwrite the defaults.
public void test_customCurrencySymbol() {
    NumberFormat nf = NumberFormat.getCurrencyInstance(Locale.US);
    DecimalFormatSymbols dfs = ((DecimalFormat) nf).getDecimalFormatSymbols();
    dfs.setCurrencySymbol("SPECIAL");
    ((DecimalFormat) nf).setDecimalFormatSymbols(dfs);
    assertEquals("SPECIAL3.14", nf.format(3.14));
    // Setting the currency again should reset the symbols.
    nf.setCurrency(Currency.getInstance("USD"));
    assertEquals("$3.14", nf.format(3.14));
    // Setting it back again should work.
    dfs.setCurrencySymbol("NEW");
    ((DecimalFormat) nf).setDecimalFormatSymbols(dfs);
    assertEquals("NEW3.14", nf.format(3.14));
}
Also used : DecimalFormatSymbols(java.text.DecimalFormatSymbols) DecimalFormat(java.text.DecimalFormat) NumberFormat(java.text.NumberFormat)

Aggregations

DecimalFormatSymbols (java.text.DecimalFormatSymbols)127 DecimalFormat (java.text.DecimalFormat)88 NumberFormat (java.text.NumberFormat)14 Locale (java.util.Locale)13 ParseException (java.text.ParseException)8 Currency (java.util.Currency)7 ObjectInputStream (java.io.ObjectInputStream)6 BigDecimal (java.math.BigDecimal)6 Test (org.junit.Test)6 IOException (java.io.IOException)5 ParsePosition (java.text.ParsePosition)5 BufferedChecksumIndexInput (org.apache.lucene.store.BufferedChecksumIndexInput)5 ChecksumIndexInput (org.apache.lucene.store.ChecksumIndexInput)5 IndexInput (org.apache.lucene.store.IndexInput)5 BytesRefBuilder (org.apache.lucene.util.BytesRefBuilder)5 ByteArrayInputStream (java.io.ByteArrayInputStream)4 ByteArrayOutputStream (java.io.ByteArrayOutputStream)4 ObjectOutputStream (java.io.ObjectOutputStream)4 BytesRef (org.apache.lucene.util.BytesRef)4 ArrayList (java.util.ArrayList)3