Java Fraction Format formatDouble(double d)

Here you can find the source of formatDouble(double d)

Description

format Double

License

Open Source License

Declaration

public static String formatDouble(double d) 

Method Source Code

//package com.java2s;
/*//from w  ww  . j  a  va  2 s  .co  m
(C) 2007 Stefan Reich (jazz@drjava.de)
This source file is part of Project Prophecy.
For up-to-date information, see http://www.drjava.de/prophecy
    
This source file is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation, version 2.1.
*/

import java.text.*;

public class Main {
    public static String formatDouble(double d) {
        return /*Double.toString(d)*/formatDouble(d, 4);
    }

    public static String formatDouble(double d, int digits) {
        String format = "0.";
        for (int i = 0; i < digits; i++)
            format += "#";
        String s = new DecimalFormat(format).format(d);
        return s.replace(',', '.'); // hack german -> english
    }

    public static String replace(String pattern, String substitute, String s) {
        StringBuffer buf = new StringBuffer();
        while (true) {
            int i = s.indexOf(pattern);
            if (i >= 0) {
                buf.append(s.substring(0, i));
                buf.append(substitute);
                s = s.substring(i + pattern.length());
            } else {
                buf.append(s);
                break;
            }
        }
        return buf.toString();
    }
}

Related

  1. formatDouble(double aValue)
  2. formatDouble(double d)
  3. formatDouble(double d)
  4. formatDouble(Double d)
  5. formatDouble(Double d)
  6. formatDouble(double d)
  7. formatDouble(Double d)
  8. formatDouble(double d, int maximumFractionDigits)
  9. formatDouble(Double d, NumberFormat format)