use of java.text.NumberFormat in project spring-framework by spring-projects.
the class NumberStyleFormatter method getNumberFormat.
@Override
public NumberFormat getNumberFormat(Locale locale) {
NumberFormat format = NumberFormat.getInstance(locale);
if (!(format instanceof DecimalFormat)) {
if (this.pattern != null) {
throw new IllegalStateException("Cannot support pattern for non-DecimalFormat: " + format);
}
return format;
}
DecimalFormat decimalFormat = (DecimalFormat) format;
decimalFormat.setParseBigDecimal(true);
if (this.pattern != null) {
decimalFormat.applyPattern(this.pattern);
}
return decimalFormat;
}
use of java.text.NumberFormat in project spring-framework by spring-projects.
the class AbstractNumberFormatter method parse.
@Override
public Number parse(String text, Locale locale) throws ParseException {
NumberFormat format = getNumberFormat(locale);
ParsePosition position = new ParsePosition(0);
Number number = format.parse(text, position);
if (position.getErrorIndex() != -1) {
throw new ParseException(text, position.getIndex());
}
if (!this.lenient) {
if (text.length() != position.getIndex()) {
// indicates a part of the string that was not parsed
throw new ParseException(text, position.getIndex());
}
}
return number;
}
use of java.text.NumberFormat in project camel by apache.
the class BindyAbstractFactory method getNumberFormat.
private static NumberFormat getNumberFormat() {
// Get instance of NumberFormat
NumberFormat nf = NumberFormat.getInstance();
// set max number of digits to 3 (thousands)
nf.setMaximumIntegerDigits(3);
nf.setMinimumIntegerDigits(3);
return nf;
}
use of java.text.NumberFormat in project camel by apache.
the class TimeUtils method printDuration.
/**
* Prints the duration in a human readable format as X days Y hours Z minutes etc.
*
* @param uptime the uptime in millis
* @return the time used for displaying on screen or in logs
*/
public static String printDuration(double uptime) {
// Code taken from Karaf
// https://svn.apache.org/repos/asf/karaf/trunk/shell/commands/src/main/java/org/apache/karaf/shell/commands/impl/InfoAction.java
NumberFormat fmtI = new DecimalFormat("###,###", new DecimalFormatSymbols(Locale.ENGLISH));
NumberFormat fmtD = new DecimalFormat("###,##0.000", new DecimalFormatSymbols(Locale.ENGLISH));
uptime /= 1000;
if (uptime < 60) {
return fmtD.format(uptime) + " seconds";
}
uptime /= 60;
if (uptime < 60) {
long minutes = (long) uptime;
String s = fmtI.format(minutes) + (minutes > 1 ? " minutes" : " minute");
return s;
}
uptime /= 60;
if (uptime < 24) {
long hours = (long) uptime;
long minutes = (long) ((uptime - hours) * 60);
String s = fmtI.format(hours) + (hours > 1 ? " hours" : " hour");
if (minutes != 0) {
s += " " + fmtI.format(minutes) + (minutes > 1 ? " minutes" : " minute");
}
return s;
}
uptime /= 24;
long days = (long) uptime;
long hours = (long) ((uptime - days) * 24);
String s = fmtI.format(days) + (days > 1 ? " days" : " day");
if (hours != 0) {
s += " " + fmtI.format(hours) + (hours > 1 ? " hours" : " hour");
}
return s;
}
use of java.text.NumberFormat in project android-saripaar by ragunathjawahar.
the class AbstractNumberValidator method getFormat.
/**
* <p>Returns a <code>NumberFormat</code> for the specified <i>pattern</i>
* and/or <code>Locale</code>.</p>
*
* @param pattern The pattern used to validate the value against or
* <code>null</code> to use the default for the <code>Locale</code>.
* @param locale The locale to use for the currency format, system default if null.
* @return The <code>NumberFormat</code> to created.
*/
protected Format getFormat(String pattern, Locale locale) {
NumberFormat formatter = null;
boolean usePattern = (pattern != null && pattern.length() > 0);
if (!usePattern) {
formatter = (NumberFormat) getFormat(locale);
} else if (locale == null) {
formatter = new DecimalFormat(pattern);
} else {
DecimalFormatSymbols symbols = new DecimalFormatSymbols(locale);
formatter = new DecimalFormat(pattern, symbols);
}
if (determineScale(formatter) == 0) {
formatter.setParseIntegerOnly(true);
}
return formatter;
}
Aggregations