Java 8 NumberFormat - 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 NumberFormat 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
Experience pair programming with AI  Get Codota for Java
This code example shows how to use the following methods:format
            return format.format(value);
        } 
 
        @Override 
        public String format(double value) {
            return format.format(value);
        } 
 
        public static class Pattern extends Number { 
 
            private static final DecimalFormatSymbols SYMBOLS = new DecimalFormatSymbols(Locale.ROOT);
 
            static final byte ID = 4;
 
            String pattern;
 
            Pattern() {} // for serialization 
 
            public Pattern(String pattern) {
                super(new DecimalFormat(pattern, SYMBOLS));
8
CodeRank
Experience pair programming with AI  Get Codota for Java
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
Experience pair programming with AI  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
Experience pair programming with AI  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
Experience pair programming with AI  Get Codota for Java
This code example shows how to use the following methods:setGroupingUsed, setMinimumIntegerDigits
 
public class TestFieldSelection extends TestCase {
 
private static NumberFormat idFormat = NumberFormat.getInstance();
  static { 
    idFormat.setMinimumIntegerDigits(4);
    idFormat.setGroupingUsed(false);
  } 
 
  public void testFieldSelection() throws Exception { 
    launch(); 
  } 
 
  public static void launch() throws Exception { 
    JobConf conf = new JobConf(TestFieldSelection.class);
    FileSystem fs = FileSystem.get(conf);
    int numOfInputLines = 10;
 
    Path OUTPUT_DIR = new Path("build/test/output_for_field_selection_test");
    Path INPUT_DIR = new Path("build/test/input_for_field_selection_test");
7
CodeRank
Experience pair programming with AI  Get Codota for Java
This code example shows how to use the following methods:format, getMaximumFractionDigits, getMinimumFractionDigits, setMaximumFractionDigits, setMinimumFractionDigits
7
CodeRank
Experience pair programming with AI  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
Experience pair programming with AI  Get Codota for Java
This code example shows how to use the following methods:setGroupingUsed, setMinimumIntegerDigits
   * sorted lexicographically, positions correspond to output partitions.*/ 
  private static final NumberFormat NUMBER_FORMAT = NumberFormat.getInstance();
  protected static final String BASE_OUTPUT_NAME = "mapreduce.output.basename";
  protected static final String PART = "part";
  static { 
    NUMBER_FORMAT.setMinimumIntegerDigits(5);
    NUMBER_FORMAT.setGroupingUsed(false);
  } 
  private FileOutputCommitter committer = null;
public static final String COMPRESS ="mapreduce.output.fileoutputformat.compress";
public static final String COMPRESS_CODEC = 
"mapreduce.output.fileoutputformat.compress.codec"; 
public static final String COMPRESS_TYPE = "mapreduce.output.fileoutputformat.compress.type";
public static final String OUTDIR = "mapreduce.output.fileoutputformat.outputdir";
 
  @Deprecated 
  public static enum Counter { 
    BYTES_WRITTEN
  } 
 
7
CodeRank
Experience pair programming with AI  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
Experience pair programming with AI  Get Codota for Java

Related Java 8 NumberFormat Questions & Answers:

See Code Examples for Java 8 NumberFormat Methods: