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));
}
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);
}
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());
}
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));
}
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));
}
Aggregations