Java tutorial
//package com.java2s; import java.util.Locale; public class Main { public static float precision(int decimalPlace, float val) { if (Float.isNaN(val)) { return 0.0f; } String str = String.format(Locale.ENGLISH, "%." + decimalPlace + 'f', val); return Float.valueOf(str); } }