Java Decimal Format toFix(double x, int dp)

Here you can find the source of toFix(double x, int dp)

Description

to Fix

License

Mozilla Public License

Declaration

public static double toFix(double x, int dp) 

Method Source Code

//package com.java2s;
/** This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 *//*from  w w w .j  a va  2s . com*/

import java.text.*;
import java.math.*;

public class Main {
    public static double toFix(double x, int dp) {
        String format = "";
        for (int i = 0; i < dp; i++) {
            format = format + "#";
        }
        return Double.parseDouble((new DecimalFormat("#0." + format)).format(x));
    }

    public static String toFix(BigDecimal x, int dp) {
        String format = "";
        for (int i = 0; i < dp; i++) {
            format = format + "#";
        }
        return (new DecimalFormat("#0." + format)).format(x);
    }
}

Related

  1. toDouble(Object obj, String pattern)
  2. toDouble(String text)
  3. toDoubleObject(Object o)
  4. toDoubOriginOutPut(double d, int digitally)
  5. toEdmDouble(double value)
  6. toFixedString(Locale locale, double value, int precision)
  7. toFmtDoubleStr(String doubleStr)
  8. toParse(double d)
  9. toPrecision(double I, int digits1, int digits2)