Code example for DecimalFormat

Methods: getDecimalFormatSymbols

0
	* Adapted from code found at currency : Java Glossary 
	* website: http://mindprod.com/jgloss/currency.html 
	********************************************************************************************/ 
	static public String convertToDollars(long pennies, boolean formatted)
	{ 
		DecimalFormat decimal = new DecimalFormat();
		 
		boolean negative;
		if ( pennies < 0 )
		{ 
			pennies = -pennies;
		    negative = true;
		} 
		else 
			negative = false;
 
		String s = Long.toString( pennies );
		// if formatted == true then we want to localize the formatting of the string for display 
		// else we are putting the string into the table and we want to NOT have formatting other 
		// than the symbol between dollars and cents. 
		String strNumber = null;
		if(formatted)
			strNumber = String.format("%,d", (pennies / 100));
		else 
			strNumber = String.format("%d", (pennies / 100));
		int len = s.length();
		switch ( len )
		{ 
			case 1: 
		        strNumber = "0" + decimal.getDecimalFormatSymbols().getDecimalSeparator() + "0" + s;
		        break; 
		    case 2: 
		        strNumber = "0" + decimal.getDecimalFormatSymbols().getDecimalSeparator() + s;
		        break; 
		    default: 
		    	strNumber = strNumber + decimal.getDecimalFormatSymbols().getDecimalSeparator() + s.substring(len-2, len);
		        break; 
		} // end switch 
		 
		if ( negative && formatted )
			strNumber = "(" + strNumber + ")";