Round half Up To Decimal String - Android java.lang

Android examples for java.lang:Double

Description

Round half Up To Decimal String

Demo Code


import java.io.PrintWriter;
import java.io.StringWriter;
import java.io.UnsupportedEncodingException;
import java.math.BigDecimal;
import java.security.NoSuchAlgorithmException;
import java.text.DecimalFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Main{

    public static String halfUpToDecimalString(int value) {
        if (value < 0) {
            return "";
        }/*from   ww  w .  ja va 2  s  .  c o m*/

        if (value == 0) {
            return "0";
        }

        if (value % 100 == 0) {
            return String.valueOf(value / 100);
        } else {
            StringBuffer strBuff = new StringBuffer();
            strBuff.append(new BigDecimal(String.valueOf(value / 100.0f))
                    .setScale(0, BigDecimal.ROUND_HALF_UP));
            return strBuff.toString();
        }
    }

}

Related Tutorials