Java Double Number to String doubleToSz(double dval)

Here you can find the source of doubleToSz(double dval)

Description

Converts double into a formatted string representation

License

Open Source License

Declaration

public static String doubleToSz(double dval) 

Method Source Code


//package com.java2s;
//License from project: Open Source License 

import java.text.*;

import java.util.*;

import java.text.*;

public class Main {
    /**/*from  w w  w  .j a  v a 2s .c  om*/
     *Converts double into a formatted string representation
     */
    public static String doubleToSz(double dval) {

        String szexp;
        double dtempval = dval;
        int nexp = 0;

        NumberFormat nf2 = NumberFormat.getInstance(Locale.ENGLISH);
        nf2.setMinimumFractionDigits(2);
        nf2.setMaximumFractionDigits(2);

        NumberFormat nf1 = NumberFormat.getInstance(Locale.ENGLISH);
        nf1.setMinimumFractionDigits(1);
        nf1.setMaximumFractionDigits(1);

        if (dval <= 0) {
            szexp = "0.00";
        } else {
            while ((dtempval < 0.995) && (dtempval > 0)) {
                nexp--;
                dtempval = dtempval * 10;
            }

            if (nexp < -2) {
                dtempval = Math.pow(10, Math.log(dval) / Math.log(10) - nexp);
                szexp = nf1.format(dtempval) + "E" + nexp;

            } else {
                szexp = nf2.format(dval);
            }
        }
        return szexp;
    }
}

Related

  1. doubleToString(double value, int fractionDigits)
  2. doubleToString(double value, int maximum, int minimum)
  3. doubleToString(double value, String format)
  4. doubleToString(final double d, final int decimalPlaces)
  5. doubleToString(final double number)
  6. doubleToVisualString(double d)