Format float for certain Decimal spaces - Android java.lang

Android examples for java.lang:Float

Description

Format float for certain Decimal spaces

Demo Code

import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.regex.Pattern;

public class Main{

    public static String getDecimalString(float source, int decimal) {
        int decimalInt = (int) Math.pow(10, decimal);
        float result = (float) (Math.round(source * decimalInt))
                / decimalInt;//from  w  w  w .  jav a  2s.c  o  m
        return String.valueOf(result);
    }

}

Related Tutorials