Java Utililty Methods Decimal Format

List of utility methods to do Decimal Format

Description

The list of methods to do Decimal Format are organized into topic(s).

Method

doubletoDecimalFromSexagesimalDegrees(final double sexagesimal)
to Decimal From Sexagesimal Degrees
final String string = getFormat().format(sexagesimal);
final int dotIndex = string.indexOf('.');
final int degrees = Integer.parseInt(string.substring(0, dotIndex));
final int minutes = Integer.parseInt(string.substring(dotIndex + 1, dotIndex + 3));
final double seconds = Double
        .parseDouble(string.substring(dotIndex + 3, dotIndex + 5) + "." + string.substring(dotIndex + 5));
double decimal;
if (sexagesimal < 0) {
...
doubletoDecimals(double d, int nrDecs)
to Decimals
String format = "#.";
while (nrDecs-- > 0)
    format += "#";
DecimalFormat decForm = new DecimalFormat(format);
return Double.valueOf(decForm.format(d));
DoubletoDouble(Object obj, String pattern)
to Double
if (obj == null) {
    return null;
} else if (obj instanceof Double) {
    return (Double) obj;
} else if (obj instanceof Number) {
    return Double.valueOf(((Number) obj).doubleValue());
} else if (obj instanceof String) {
    return toDouble((String) obj);
...
DoubletoDouble(String text)
Convertesc un string in Double
if (isValidNumber(text)) {
    ParsePosition p = new ParsePosition(0);
    Number number = getDecimalFormatter(formaterScale).parse(text, p);
    return new Double(number.doubleValue());
} else
    return null;
DoubletoDoubleObject(Object o)
to Double Object
if (o == null) {
    return null;
Double num;
if (o instanceof Double) {
    num = (Double) o;
} else if (o instanceof Number) {
    num = Double.valueOf(((Number) o).doubleValue());
...
StringtoDoubOriginOutPut(double d, int digitally)
to Doub Origin Out Put
DecimalFormat df = null;
String dig = null;
digitally = (digitally == 0) ? digitally++ : digitally;
try {
    df = (DecimalFormat) NumberFormat.getInstance(Locale.US);
} catch (ClassCastException e) {
    System.err.println(e);
switch (digitally) {
case 1:
    dig = "####0.0";
    break;
case 2:
    dig = "####0.00";
    break;
case 3:
    dig = "###,##0.000";
    break;
case 4:
    dig = "####0.0000";
    break;
default:
    dig = "####0.00";
df.applyPattern(dig);
return df.format(d);
StringtoEdmDouble(double value)
Convert the given value to the String representation of a EDM Double value.
return doubleFormat.format(value);
doubletoFix(double x, int dp)
to Fix
String format = "";
for (int i = 0; i < dp; i++) {
    format = format + "#";
return Double.parseDouble((new DecimalFormat("#0." + format)).format(x));
StringtoFixedString(Locale locale, double value, int precision)
to Fixed String
NumberFormat nf = NumberFormat.getInstance(locale);
int current = nf.getMaximumFractionDigits();
nf.setMaximumFractionDigits(precision);
String ans = nf.format(value);
nf.setMaximumFractionDigits(current);
return ans;
StringtoFmtDoubleStr(String doubleStr)
to Fmt Double Str
return toFmtDoubleStr(Double.parseDouble(doubleStr));