Java Fraction Format formatSpeed(float speed)

Here you can find the source of formatSpeed(float speed)

Description

Format a floating point to a String with a 1 digit fraction.

License

LGPL

Parameter

Parameter Description
speed a parameter

Return

String A formated speed string with one digit fraction looking like 0.1

Declaration

public static String formatSpeed(float speed) 

Method Source Code

//package com.java2s;
//License from project: LGPL 

import java.text.*;

public class Main {
    /**/*from   w w  w .j  a v  a  2 s . com*/
     * Format a floating point to a String with a 1 digit fraction.
     * 
     * @param speed
     * @return String A formated speed string with one digit fraction looking
     *         like 0.1
     */
    public static String formatSpeed(float speed) {
        NumberFormat formatter = NumberFormat.getInstance();

        if (speed < 1) {
            formatter.setMaximumFractionDigits(2);
            formatter.setMinimumFractionDigits(2);
        } else {
            formatter.setMaximumFractionDigits(1);
            formatter.setMinimumFractionDigits(1);
        }
        String format = formatter.format(speed);
        return format;
    }
}

Related

  1. formatPowerFloat(float averageRfTickSent)
  2. formatPrice(Double price)
  3. formatQuantity(Double quantity)
  4. formatRate(double events, double time, String event)
  5. formatSimpleDecimal(double d)
  6. formatted_string(double number)
  7. formatToEightPlaces(double pValue)
  8. formatToString(Double v)
  9. formatTSYS(double tsys)