Convert double to a string with correct separator and precision - Android java.text

Android examples for java.text:DecimalFormat

Description

Convert double to a string with correct separator and precision

Demo Code


//package com.java2s;
import java.text.DecimalFormat;
import java.text.DecimalFormatSymbols;

public class Main {
    /**/*from w w w. java 2s  .  c  om*/
     * Convert double to a string with correct separator and precision
     * @param val - double to convert
     * @return
     */
    public static String getStringDouble(Double val) {
        DecimalFormatSymbols df = new DecimalFormatSymbols();
        df.setDecimalSeparator('.'); // Numeric keyboard has only point, so we
        // should use it
        DecimalFormat twoDigitsFormat = new DecimalFormat("#.##", df);
        return twoDigitsFormat.format(val);
    }
}

Related Tutorials