Java - Write code to Convert a float to a string with format

Requirements

Write code to Convert a float to a string with format

Demo

//package com.book2s;
import java.text.DecimalFormat;

public class Main {
    public static void main(String[] argv) {
        float value = 42.45678f;
        String format = "#.00";
        System.out.println(floatToString(value, format));
    }/*  ww  w .  ja v  a  2 s  .  c o  m*/

    /**
     * Converts a float to a string.
     * 
     * @param value
     * @param format
     *          The format, e.g. #.00
     * @return
     */
    public static String floatToString(float value, String format) {
        return doubleToString(value, format);
    }

    /**
     * Converts a double to a string.
     * 
     * @param value
     * @param format
     *          The format, e.g. #.00
     * @return
     */
    public static String doubleToString(double value, String format) {
        return new DecimalFormat(format).format(value);
    }
}

Related Exercise