Java Parse Double parseToEngineeringNotation(double val, int decimalHouses)

Here you can find the source of parseToEngineeringNotation(double val, int decimalHouses)

Description

parse To Engineering Notation

License

Open Source License

Parameter

Parameter Description
val a parameter
decimalHouses a parameter

Declaration

public static String parseToEngineeringNotation(double val, int decimalHouses) 

Method Source Code

//package com.java2s;
/*/* w ww  .  j a  va  2  s  . co m*/
 * Copyright (c) 2004-2013 Universidade do Porto - Faculdade de Engenharia
 * Laborat?rio de Sistemas e Tecnologia Subaqu?tica (LSTS)
 * All rights reserved.
 * Rua Dr. Roberto Frias s/n, sala I203, 4200-465 Porto, Portugal
 *
 * This file is part of Neptus, Command and Control Framework.
 *
 * Commercial Licence Usage
 * Licencees holding valid commercial Neptus licences may use this file
 * in accordance with the commercial licence agreement provided with the
 * Software or, alternatively, in accordance with the terms contained in a
 * written agreement between you and Universidade do Porto. For licensing
 * terms, conditions, and further information contact lsts@fe.up.pt.
 *
 * European Union Public Licence - EUPL v.1.1 Usage
 * Alternatively, this file may be used under the terms of the EUPL,
 * Version 1.1 only (the "Licence"), appearing in the file LICENCE.md
 * included in the packaging of this file. You may not use this work
 * except in compliance with the Licence. Unless required by applicable
 * law or agreed to in writing, software distributed under the Licence is
 * distributed on an "AS IS" basis, WITHOUT WARRANTIES OR CONDITIONS OF
 * ANY KIND, either express or implied. See the Licence for the specific
 * language governing permissions and limitations at
 * https://www.lsts.pt/neptus/licence.
 *
 * For more information please see <http://lsts.fe.up.pt/neptus>.
 *
 * Author: 
 * 13/Jun/2005
 */

import java.text.DecimalFormat;

public class Main {
    /**
     * @param val
     * @param decimalHouses
     * @return
     */
    public static String parseToEngineeringNotation(double val, int decimalHouses) {
        DecimalFormat engNot = new DecimalFormat("##0.###E0");
        String pl = engNot.format(val);
        String[] pl2 = pl.split("E");
        double vl = Double.parseDouble(pl2[0].replace(',', '.'));
        vl = round(vl, decimalHouses);
        int mul = Integer.parseInt(pl2[1]);
        String mulStr = "";
        switch (mul) {
        case 24:
            mulStr = "Y";
            break;
        case 21:
            mulStr = "Z";
            break;
        case 18:
            mulStr = "E";
            break;
        case 15:
            mulStr = "P";
            break;
        case 12:
            mulStr = "T";
            break;
        case 9:
            mulStr = "G";
            break;
        case 6:
            mulStr = "M";
            break;
        case 3:
            mulStr = "k";
            break;
        case -3:
            mulStr = "m";
            break;
        case -6:
            mulStr = "u";
            mulStr = "\u00B5";
            break;
        case -9:
            mulStr = "n";
            break;
        case -12:
            mulStr = "p";
            break;
        case -15:
            mulStr = "f";
            break;
        case -18:
            mulStr = "a";
            break;
        case -21:
            mulStr = "z";
            break;
        case -24:
            mulStr = "y";
            break;
        default:
            mulStr = "";
            break;
        }

        return (decimalHouses == 0 ? (long) vl + "" : vl) + mulStr;
    }

    /**
     * @param val
     * @param decimalHouses
     * @return
     */
    public static double round(double val, int decimalHouses) {
        double base = Math.pow(10d, decimalHouses);
        double result = Math.round(val * base) / base;
        return result;
    }

    /**
     * @param val
     * @param decimalHouses
     * @return
     */
    public static float round(float val, int decimalHouses) {
        float base = (float) Math.pow(10f, decimalHouses);
        float result = Math.round(val * base) / base;
        return result;
    }
}

Related

  1. parseDouble(String value)
  2. ParseDoubleEx(String s, double value_if_fault)
  3. parseDoubleInCurrentDefaultLocale(String text)
  4. parseInternationalDouble(String number)
  5. parseToCientificNotation(double value)
  6. parseYuanToYi(Double data)