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);
Full Snippet Info
8
CodeRank
Stop searching for code, let great code find you!  Add Codota to your java IDE
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));
Full Snippet Info
8
CodeRank
Stop searching for code, let great code find you!  Add Codota to your java IDE
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);
    } 
 
Full Snippet Info
8
CodeRank
Stop searching for code, let great code find you!  Add Codota to your java IDE
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
Stop searching for code, let great code find you!  Add Codota to your java IDE
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));
Full Snippet Info
7
CodeRank
Stop searching for code, let great code find you!  Add Codota to your java IDE

Related Java 8 NumberFormat Questions & Answers:

See Code Examples for Java 8 NumberFormat Methods: