Java examples for java.lang:double Format
This method is to convert a cent to dollar amount given String as input
import java.math.BigDecimal; import java.util.StringTokenizer; public class Main{ /**/*from w ww . j a va 2s . c o m*/ * This method is to convert a cent to dollar amount given String as input * * @param amount * @return */ public static String centsToDollar(String amount) { if (amount.length() < 2) { amount = PadderUtil.leftPad(amount, 3, '0'); } String firstStr = amount.substring(0, amount.length() - 2); String secondStr = amount.substring(amount.length() - 2, amount.length()); // String finalStr = firstStr + "." + secondStr; return firstStr + "." + secondStr; } /** * This method is to convert a cent to dollar amount given double as input * * @param amount * @return */ public static double centsToDollar(long amount) { String amountStr = ((Long) amount).toString(); if (amountStr.length() > 1) { String firstStr = amountStr .substring(0, amountStr.length() - 2); String secondStr = amountStr.substring(amountStr.length() - 2, amountStr.length()); String finalStr = firstStr + "." + secondStr; // double doubleValue = Double.parseDouble(finalStr); return Double.parseDouble(finalStr); } else if (amountStr.length() == 1) { return (amount / 100d); } else { return 0; } } }