Java tutorial
//package com.java2s; import java.math.BigDecimal; public class Main { public static String keepNSecimal(String numberStr, int n) { double number = Double.parseDouble(numberStr); BigDecimal b = new BigDecimal(number); String ss = b.setScale(n, BigDecimal.ROUND_HALF_UP).doubleValue() + ""; int pointIndex = ss.lastIndexOf("."); int fractionCount = ss.length() - pointIndex - 1; for (int i = 0; i < n - fractionCount; i++) { ss += 0; } return ss; } }