Java 8 DecimalFormat - top ranked examples from Open Source projects

These code examples were ranked by Codota’s semantic indexing as the best open source examples for Java 8 DecimalFormat class.

This code example shows how to use the following methods:getDecimalFormatSymbols, setDecimalFormatSymbols, format
import java.util.Locale; 
import tests.support.Support_Locale; 
 
public class DecimalFormatTest extends junit.framework.TestCase {
    public void test_exponentSeparator() throws Exception { 
        DecimalFormat df = new DecimalFormat("0E0");
        assertEquals("1E4", df.format(12345.));
 
        DecimalFormatSymbols dfs = df.getDecimalFormatSymbols();
        dfs.setExponentSeparator("-useless-api-");
        df.setDecimalFormatSymbols(dfs);
        assertEquals("1-useless-api-4", df.format(12345.));
    } 
 
    public void test_setMaximumFractionDigitsAffectsRoundingMode() throws Exception { 
        DecimalFormat df = (DecimalFormat) DecimalFormat.getInstance(Locale.US);
        df.setMaximumFractionDigits(0);
        df.setRoundingMode(RoundingMode.HALF_UP);
        assertEquals("-0", df.format(-0.2));
        df.setMaximumFractionDigits(1);
8
CodeRank
Connect your IDE to all the code out there  Get Codota for Java
This code example shows how to use the following methods:setCurrency, setMaximumFractionDigits, setMinimumFractionDigits, setParseBigDecimal, setRoundingMode
This code example shows how to use the following methods:setDecimalFormatSymbols, format
        DecimalFormatSymbols dfs = new DecimalFormatSymbols();
        dfs.setDecimalSeparator(',');
        dfs.setGroupingSeparator(' ');
        DecimalFormat currency;
        if (!round) {
            currency = new DecimalFormat("#,##0.00 ");
        } 
        else { 
            currency = new DecimalFormat("#,##0 ");  
        } 
        currency.setDecimalFormatSymbols(dfs);
        return currency.format(balance.doubleValue())+curr;
    } 
    public static String formatBalance(BigDecimal balance, String curr) {
        return formatBalance(balance, curr, false);
    } 
    public static String formatBalance(Double balance, String curr) {
        return formatBalance(new BigDecimal(balance), curr);
    } 
 
8
CodeRank
Connect your IDE to all the code out there  Get Codota for Java
This code example shows how to use the following methods:format, setGroupingUsed, setMaximumFractionDigits, setMinimumFractionDigits, setMinimumIntegerDigits
import java.text.DecimalFormat; 
import java.text.NumberFormat; 
 
public class Format { 
	public static void main(String[] args){
		NumberFormat numForm = new DecimalFormat();
		numForm.setMinimumIntegerDigits(9);
		//Maximum also available for Integer digits and Fraction digits 
		numForm.setGroupingUsed(false);//stops it from inserting commas
		System.out.println(numForm.format(7.125));
		 
		//example of Fraction digit options 
		numForm.setMinimumIntegerDigits(5);
		numForm.setMinimumFractionDigits(5);
		System.out.println(numForm.format(7.125));
		numForm.setMinimumFractionDigits(0);
		numForm.setMaximumFractionDigits(2);
		System.out.println(numForm.format(7.125));
		System.out.println(numForm.format(7.135));//rounds to even
	} 

8
CodeRank
Connect your IDE to all the code out there  Get Codota for Java
This code example shows how to use the following methods:parseObject, format, getMaximumFractionDigits, parse, isParseIntegerOnly
Locale arLocale = new Locale("ar", "AE");
        if (Support_Locale.isLocaleAvailable(arLocale)) {
            format = (DecimalFormat) NumberFormat.getIntegerInstance(arLocale);
            assertEquals("#,##0;#,##0-", format.toPattern());
            assertEquals("\u0666-", format.format(-6));
            assertEquals(new Long(-36), format.parse("36-"));
            assertEquals(new Long(-36), format.parseObject("36-"));
            assertEquals(0, format.getMaximumFractionDigits());
            assertTrue(format.isParseIntegerOnly());
        } 
    } 
 
    public void test_setMaximumIntegerDigits() { 
        NumberFormat format = NumberFormat.getInstance();
        format.setMaximumIntegerDigits(2);
        assertEquals("Wrong result: case 1", "23", format.format(123));
 
        format.setMaximumIntegerDigits(Integer.MIN_VALUE);
        assertEquals("Wrong result: case 2", "0", format.format(123));
7
CodeRank
Connect your IDE to all the code out there  Get Codota for Java
This code example shows how to use the following methods:setDecimalFormatSymbols, setMaximumFractionDigits, setMinimumFractionDigits
  //  Private Classes 
  //******************************************************************** 
 
  private DecimalFormat simpleFormatterCreator() {
    String fms = creator();
    DecimalFormat formater = new DecimalFormat(fms);  
 
    DecimalFormatSymbols dfs = ERXDecimalFormatSymbols.decimalFormatSymbols(decimal_point());
    formater.setDecimalFormatSymbols(dfs);
 
    int i = log10(scale());
    formater.setMinimumFractionDigits(i);
    formater.setMaximumFractionDigits(i);
 
    return formater;
  } 
 
  private DecimalFormat formatterCreator() {
    String fms = creator();
    DecimalFormat formater = new DecimalFormat(prefixSymbol() + fms + suffixSymbol());  
8
CodeRank
Connect your IDE to all the code out there  Get Codota for Java
This code example shows how to use the following methods:applyPattern, format, parse
            args = {java.lang.String.class}
        ) 
    }) 
    @AndroidOnly("special feature of icu4c") 
    public void test_paddingPattern() throws Exception { 
        format.applyPattern("*x##,##,#,##0.0#");
        assertEquals("xxxxxxxxx123.0", format.format(123));
        assertEquals(123, format.parse("xxxxxxxxx123.0").intValue());
 
        format.applyPattern("$*x#,##0.00");
        assertEquals("$xx123.00", format.format(123));
        assertEquals("$1,234.00", format.format(1234));
 
        format.applyPattern("*\u00e7#0 o''clock");
        assertEquals("\u00e72 o'clock", format.format(2));
        assertEquals("12 o'clock", format.format(12));
        assertEquals(2, format.parse("\u00e72 o'clock").intValue());
        assertEquals(12, format.parse("12 o'clock").intValue());
 
        try { 
7
CodeRank
Connect your IDE to all the code out there  Get Codota for Java
This code example shows how to use the following methods:setDecimalFormatSymbols, setMinimumFractionDigits, format
            return String.valueOf(number);
        } 
        if (fractionDigits < 0 || fractionDigits > 20) {
            throw new ThrowException(context, context.createRangeError("Number.prototype.toExponential() [fractionDigits] must be between 0 and 20"));
        } 
        DecimalFormat decimalFormat = new DecimalFormat("0.00##################E0");
        decimalFormat.setDecimalFormatSymbols(new DecimalFormatSymbols(Locale.US));
        decimalFormat.setMinimumFractionDigits(fractionDigits.intValue());
        return Types.rewritePossiblyExponentialValue(decimalFormat.format(number.doubleValue()));
    } 
} 
7
CodeRank
Connect your IDE to all the code out there  Get Codota for Java
This code example shows how to use the following methods:format
 
  private static void delete(String tableName, Connection conn) throws SQLException {
    Statement stmt = null;
    try { 
      stmt = conn.createStatement();
      DecimalFormat df = new DecimalFormat("0000000000");
      String rowKey = df.format(1);
      stmt.executeUpdate("delete col1 from " + tableName + " where rowkey = '" + rowKey + "'");
       
      rowKey = df.format(2);
      stmt.executeUpdate("delete col1 from " + tableName + " where rowkey = '" + rowKey + "' and col1 = 'ck1_2'");
 
      rowKey = df.format(5);
      stmt.executeUpdate("delete * from " + tableName + " where rowkey = '" + rowKey + "'");
    } finally { 
      if(stmt != null) {
        stmt.close();
      } 
    } 
  } 
7
CodeRank
Connect your IDE to all the code out there  Get Codota for Java
This code example shows how to use the following methods:setDecimalSeparatorAlwaysShown, setMaximumFractionDigits, setMinimumFractionDigits, format
   * @param digits 
   *          the number of digits past the decimal point 
   * @return a string representation of the percentage 
   */ 
  public static String formatPercent(double done, int digits) {
    DecimalFormat percentFormat = new DecimalFormat("0.00%");
    double scale = Math.pow(10.0, digits + 2);
    double rounded = Math.floor(done * scale);
    percentFormat.setDecimalSeparatorAlwaysShown(false);
    percentFormat.setMinimumFractionDigits(digits);
    percentFormat.setMaximumFractionDigits(digits);
    return percentFormat.format(rounded / scale);
  } 
 
  /** 
   * Given an array of strings, return a comma-separated list of its elements. 
   *  
   * @param strs 
   *          Array of strings 
   * @return Empty string if strs.length is 0, comma separated list of strings 
7
CodeRank
Connect your IDE to all the code out there  Get Codota for Java

Related Java 8 DecimalFormat Questions & Answers:

See Code Examples for Java 8 DecimalFormat Methods: