Java Fraction Format format(double number, int digits)

Here you can find the source of format(double number, int digits)

Description

Use this method like this:
String formatted = Util.format(1234.67789, 2);
formatted ==> "1234.68"

License

Apache License

Parameter

Parameter Description

Return

formatted double

Declaration

public static double format(double number, int digits) 

Method Source Code

//package com.java2s;
/**/*  www.  j  a v a 2  s. c om*/
 * Copyright 2014 tgrape Inc.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *     http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import java.text.DecimalFormat;

public class Main {
    /**
    * relation fuction to Currency format<br>
    * <font color="#0000ff">String formatted = Util.format("1234.67789", 2);<br>
    * formatted ==> "1234.68"
    * </font>
    * @param String<br>
    * @param int<br>
    * @return formatted double
    */
    public static double format(String str_number, int digits) {
        String pattern = "###.";
        double value = -1.;
        for (int i = 0; i < digits; i++) {
            pattern += "#";
        }
        try {
            value = Double.parseDouble(str_number);
        } catch (NumberFormatException e) {
            System.err.println("[Util] Bad Number String!!");
        }
        return customFormat(pattern, value);
    }

    /**
    * Use this method like this:<br>
    * <font color="#0000ff">String formatted = Util.format(1234.67789, 2);<br>
    * formatted ==> "1234.68"
    * </font>
    * @param numer<br>
    * @param digits<br>
    * @return formatted double
    */
    public static double format(double number, int digits) {
        return format("" + number, digits);
    }

    /**
    * This method actually does all for number formatting...<br>
    * @param pattern pattern to apply to the given double value<br>
    * @param value number to be formatted<br>
    * @return formatted double
    */
    private static double customFormat(String pattern, double value) {
        DecimalFormat myFormatter = new DecimalFormat(pattern);
        try {
            String output = myFormatter.format(value);
            return Double.parseDouble(output);
        } catch (NumberFormatException e) {
            System.err.println("[Util] at customFormat(..): NumberFormatException");
            return -1.0;
        }
    }
}

Related

  1. format(double num, int n)
  2. format(double num, String pattern)
  3. format(double number)
  4. format(double number)
  5. format(double number)
  6. format(double number, String fmt)
  7. format(double price)
  8. format(double val)
  9. format(double val)